将UIImageView中的图像保存到iPad相册

7
我正在创建一个iPad应用程序,其中在水平滚动视图中有几张图片(UIImageViews)。我希望允许用户在点击其中一张UIImageView时将图像保存到他们的照片库中。我喜欢Safari处理此问题的方式:只需轻触并保持按住,直到弹出菜单出现,然后单击保存图像。我知道有"UIImageWriteToSavedPhotosAlbum"。但我是iOS开发的新手,不太确定该如何使用它以及在哪里放置它(即如何检测点击了哪个图像)。
从我所找到的资料来看,我看到人们使用UIImage而不是UIImageView。我需要将我的视图转换为UIImage吗?如果是这样,怎么做?如何检测用户何时点击图像以及点击了哪个UIImageView?如果您能指点我正确的方向,并提供一些示例,我将不胜感激。
4个回答

32
你可以使用UIImageViewimage属性来获取当前的图像:
UIImage* imageToSave = [imageView image]; // alternatively, imageView.image

// Save it to the camera roll / saved photo album
UIImageWriteToSavedPhotosAlbum(imageToSave, nil, nil, nil);

谢谢您的快速回复。但是我如何检测到哪个UIImageView被点击了呢?有没有一种方法可以在用户点击图像时弹出一个弹出窗口,让用户选择保存按钮?当“保存”按钮被按下时,我会将您的语句放在那个IBAction中,对吗? - Brian
您可以在Interface Builder中将“touch up inside”事件与您实现的操作方法(也可以通过编程完成)连接到UIImageView上,例如-(void)touchedImageView:(id)sender,其中sender是被触摸的视图。从那里,您可以呈现一个菜单(例如UIActionSheet),以决定是否保存图像。 - bosmacs
我已经编写好代码,但在Interface Builder中没有看到任何“touch”事件。当我选择其中一个UIImageView时,我进入Inspector窗口,然后是Connections选项卡,对吧?当选择按钮时,我可以看到触摸事件,但是UIImageView则不行。我应该将所有这些UIImageView都转换成按钮吗? - Brian
抱歉,我忘记了。您可以尝试这个问题中的建议:https://dev59.com/6nRA5IYBdhLWcg3wtwSe -- 要么子类化UIImageView以覆盖touch*方法,要么在每个UIImageView上放置一个隐藏的按钮,并将触摸事件连接到该按钮。 - bosmacs
我昨天试着玩了一下,但我的问题是这在一个滚动视图中。所以如果用户先触摸按钮然后移动滚动条,它不会滚动。我在网上搜索了一下这个问题,但还没有找到有效的解决方法。不过我还在继续努力。谢谢你的帮助。 - Brian
显示剩余3条评论

4
- (IBAction)TakePicture:(id)sender {


    // Create image picker controller
    UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];

    // Set source to the camera
    imagePicker.sourceType =  UIImagePickerControllerSourceTypeCamera;


    // Delegate is self
    imagePicker.delegate = self;


    OverlayView *overlay = [[OverlayView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGTH)];

   // imagePicker.cameraViewTransform = CGAffineTransformScale(imagePicker.cameraViewTransform, CAMERA_TRANSFORM_X, CAMERA_TRANSFORM_Y);

    // Insert the overlay:
    imagePicker.cameraOverlayView = overlay;

   // Allow editing of image ?
    imagePicker.allowsImageEditing = YES;
    [imagePicker setCameraDevice:
     UIImagePickerControllerCameraDeviceFront];
    [imagePicker setAllowsEditing:YES];
    imagePicker.showsCameraControls=YES;
    imagePicker.navigationBarHidden=YES;
    imagePicker.toolbarHidden=YES;
    imagePicker.wantsFullScreenLayout=YES;


    self.library = [[ALAssetsLibrary alloc] init];


    // Show image picker
    [self presentModalViewController:imagePicker animated:YES];
}





- (void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    // Access the uncropped image from info dictionary
    UIImage *image = [info objectForKey:@"UIImagePickerControllerOriginalImage"];




    // Save image to album
    UIImageWriteToSavedPhotosAlbum(image, self, @selector(image:didFinishSavingWithError:contextInfo:), nil);


    // save image to custom album
    [self.library saveImage:image toAlbum:@"custom name" withCompletionBlock:^(NSError *error) {
        if (error!=nil) {
            NSLog(@"Big error: %@", [error description]);
        }
    }];

    [picker dismissModalViewControllerAnimated:NO];


}



- (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo
{
    UIAlertView *alert;

    // Unable to save the image  
    if (error)
        alert = [[UIAlertView alloc] initWithTitle:@"Error" 
                                           message:@"Unable to save image to Photo Album." 
                                          delegate:self cancelButtonTitle:@"Ok" 
                                 otherButtonTitles:nil];
    else // All is well
        alert = [[UIAlertView alloc] initWithTitle:@"Success" 
                                           message:@"Image saved to Photo Album." 
                                          delegate:self cancelButtonTitle:@"Ok" 
                                 otherButtonTitles:nil];


    [alert show];
}



- (void) alertView:(UIAlertView *)alert clickedButtonAtIndex:(NSInteger)buttonIndex
{
    // After saving iamge, dismiss camera
    [self dismissModalViewControllerAnimated:YES];
}

3

关于您问题中询问如何检测哪个UIImageView被点击,您可以使用以下代码:

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{

 [super touchesEnded:touches withEvent:event];
 UITouch *touch = [touches anyObject];
 CGPoint touchEndpoint = [touch locationInView:self.view]; 
 CGPoint imageEndpoint = [touch locationInView:imageview];
 if(CGRectContainsPoint([imageview frame], touchEndpoint))
 {
 do here any thing after touch the event.

  }
}

1
Swift 中:
    // Save it to the camera roll / saved photo album
    // UIImageWriteToSavedPhotosAlbum(self.myUIImageView.image, nil, nil, nil) or 
    UIImageWriteToSavedPhotosAlbum(self.myUIImageView.image, self, "image:didFinishSavingWithError:contextInfo:", nil)

    func image(image: UIImage!, didFinishSavingWithError error: NSError!, contextInfo: AnyObject!) {
            if (error != nil) {
                // Something wrong happened.
            } else {
                // Everything is alright.
            }
    }

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接