Save Image to UIImageView in iPad Photos Library

I am creating an iPad application with multiple images ( UIImageViews ) in horizontal scrolling. I want the user to be able to save images in their Photo Library when they click on one of UIImageView s. I love how Safari deals with this issue: just press and hold until a pop-up menu appears, and then click Save Image. I know there is " UIImageWriteToSavedPhotosAlbum ". But I'm new to iOS development, and I'm not too sure where to go with it and where to put it (i.e. How to determine which image was listened to).

From what I found, I saw people use UIImage instead of UIImageView . Do I need to convert my view to UIImage ? If so, how? How to determine when a user deletes images, and which UIImageView was used? If you could point me in the right direction and possibly some examples, I would really appreciate it.

+7
iphone xcode ipad uiimageview
source share
4 answers

You can use the image property for UIImageView to get the current image:

 UIImage* imageToSave = [imageView image]; // alternatively, imageView.image // Save it to the camera roll / saved photo album UIImageWriteToSavedPhotosAlbum(imageToSave, nil, nil, nil); 
+32
source share
 - (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]; } 
+4
source share

As for the part of your question that asks how to determine which UIImageView was used, you can use the following code:

 -(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. } } 
+3
source share

In 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. } } 
+1
source share

All Articles