7

我正在创建一个 iPad 应用程序,它UIImageViews在水平滚动视图中有几张图片 ( )。我想让用户在点击其中一个时能够将图像保存到他们的照片库中UIImageView。我喜欢 Safari 处理此问题的方式:您只需点击并按住直到出现弹出菜单,然后单击保存图像。我知道有“ UIImageWriteToSavedPhotosAlbum”。但我是 iOS 开发的新手,我不太确定该去哪里以及放在哪里(即如何检测哪个图像被点击)。

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

4

4 回答 4

32

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

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

// Save it to the camera roll / saved photo album
UIImageWriteToSavedPhotosAlbum(imageToSave, nil, nil, nil);
于 2010-11-16T19:43:03.737 回答
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];
}
于 2012-11-06T15:06:55.080 回答
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.

  }
}
于 2011-07-28T19:27:56.977 回答
1

斯威夫特

    // 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.
            }
    }
于 2015-03-18T18:12:09.497 回答