Iphone 将UIImageView中的图像保存到iPad照片库

Iphone 将UIImageView中的图像保存到iPad照片库,iphone,xcode,ipad,uiimageview,photolibrary,Iphone,Xcode,Ipad,Uiimageview,Photolibrary,我正在创建一个iPad应用程序,它在水平滚动视图中有几个图片。我希望用户能够在点击其中一个UIImageView时将图像保存到他们的照片库中。我喜欢Safari处理这件事的方式:您只需按住鼠标,直到弹出菜单出现,然后单击“保存图像”。我知道有UIImageWriteToSavedPhotosAlbum。但我是iOS开发的新手,我不太确定该把它放在哪里,也不知道该如何检测哪个图像被点击 根据我的发现,我看到人们使用UIImage而不是UIImageView。是否需要将视图转换为UIImage?如果

我正在创建一个iPad应用程序,它在水平滚动视图中有几个图片。我希望用户能够在点击其中一个UIImageView时将图像保存到他们的照片库中。我喜欢Safari处理这件事的方式:您只需按住鼠标,直到弹出菜单出现,然后单击“保存图像”。我知道有UIImageWriteToSavedPhotosAlbum。但我是iOS开发的新手,我不太确定该把它放在哪里,也不知道该如何检测哪个图像被点击

根据我的发现,我看到人们使用UIImage而不是UIImageView。是否需要将视图转换为UIImage?如果是,怎么做?如何检测用户何时点击图像,以及点击了哪个UIImageView?如果你能给我指出正确的方向,也许还有一些例子,我会非常感激

您可以使用UIImageView的image属性获取当前图像:

UIImage* imageToSave = [imageView image]; // alternatively, imageView.image

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

关于问题中询问如何检测哪个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.

  }
}
迅速:


谢谢你的快速回复。但我如何检测哪个UIImageView被点击?当用户点击图像时,是否有一种方法可以使弹出窗口出现,从而允许用户选择一个按钮进行保存?当按下“保存”按钮时,我会将您的语句放在该iAction中,对吗?您可以将Interface Builder中UIImageView上的“touch up inside”事件连接到一个操作方法,您也可以通过编程方式实现此操作,例如-voidtouchedImageView:idsender,在这种情况下,发送方将是被触摸的视图。从那里,您可以显示一个菜单(如UIActionSheet)来决定是否保存图像。我已经记下了代码,但在Interface Builder中没有看到任何“触摸”事件。选择其中一个UIImageView后,我进入Inspector窗口,然后进入Connections选项卡,对吗?当选择按钮而不是UIImageView时,我会看到触摸事件。我应该把所有这些UIImageView都做成按钮吗?对不起,忘了。您可以尝试这个问题中的建议:-要么子类UIImageView来覆盖touch*方法,要么在每个UIImageView上放置一个隐藏按钮,并将触摸事件连接到按钮。我昨天对此进行了一些讨论,但我的问题是这是在滚动视图中。因此,如果用户先触摸按钮,然后移动到滚动,则不会滚动。我在谷歌上搜索了一下这个问题,但我还没有找到任何有效的方法。不过还在努力。谢谢你的帮助。
- (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];
}
    // 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.
            }
    }