IPhone: How to get the path to an image file saved using UIImageWriteToSavedPhotosAlbum ()?

I save the merged image in the iPhone photo library using:

UIImageWriteToSavedPhotosAlbum(viewImage, self, @selector(savedPhotoImage:didFinishSavingWithError:contextInfo:), nil); 

And getting a callback using:

 - (void) savedPhotoImage:(UIImage*)image didFinishSavingWithError:(NSError *)error contextInfo: (void *)contextInfo { NSLog(@"%@", [error localizedDescription]); NSLog(@"info: %@", contextInfo);} 

I would like to get a path for where the image was saved, so I can add it to an array that will be used to call up a list of saved items elsewhere in the application.

When I upload an image using the collector, it displays the path information. However, when I save the created image, I cannot find where to drag the saved image path.

I have an Internet search, but most examples dwell on a callback with a good message to say that the image was successfully saved. I just wanted to know where he was saved.

I understand that one way could be to define my own paths, but since the method does this for me, I just hoped it could tell me where it was stored.

+44
ios iphone save uiimage
Dec 16 2018-10-12T00:
source share
7 answers

I finally found out the answer. UIImage methods seem to highlight metadata, so using UIImageWriteToSavedPhotosAlbum is not good.

However, Apple’s ios4 added a new framework for processing a photo library called ALAssetsLibrary.

First you need to right-click on the objects and in the assembly part, add the AlAsset Framework to your project using the "+" icon in the lower left.

Then add #import "AssetsLibrary/AssetsLibrary.h"; into the header file of your class.

Finally, you can use the following code:

 UIImage *viewImage = YOUR UIIMAGE // --- mine was made from drawing context ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init]; // Request to save the image to camera roll [library writeImageToSavedPhotosAlbum:[viewImage CGImage] orientation:(ALAssetOrientation)[viewImage imageOrientation] completionBlock:^(NSURL *assetURL, NSError *error){ if (error) { NSLog(@"error"); } else { NSLog(@"url %@", assetURL); } }]; [library release]; 

And this is the path to the file you just saved.

+85
Dec 16 2018-10-12T00:
source share

My code

 -(void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{ UIImage *image = [info valueForKey:UIImagePickerControllerOriginalImage]; imageURL = nil; ALAssetsLibraryWriteImageCompletionBlock completeBlock = ^(NSURL *assetURL, NSError *error){ if (!error) { #pragma mark get image url from camera capture. imageURL = [NSString stringWithFormat:@"%@",assetURL]; } }; if(image){ ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init]; [library writeImageToSavedPhotosAlbum:[image CGImage] orientation:(ALAssetOrientation)[image imageOrientation] completionBlock:completeBlock]; } } 

in .h import the library and determine the type def from ALAssetsLibraryWriteImageCompletionBlock

 #import <UIKit/UIKit.h> #import <AssetsLibrary/AssetsLibrary.h> typedef void (^ALAssetsLibraryWriteImageCompletionBlock)(NSURL *assetURL, NSError *error); 

if you do not know how to get <AssetsLibrary/AssetsLibrary.h> , add an existing structure (AssetsLibrary.framework)

+3
Mar 18 '11 at 19:31
source share

Fast version of version OlivaresF version 4.1

  PHPhotoLibrary.shared().performChanges({ PHAssetChangeRequest.creationRequestForAsset(from: image) }) { (success, error) in if success { } else { } } 
+2
Jul 05 '18 at 15:34
source share

Swift Version will be

  ALAssetsLibrary().writeImageToSavedPhotosAlbum(editedImage.CGImage, orientation: ALAssetOrientation(rawValue: editedImage.imageOrientation.rawValue)!, completionBlock:{ (path:NSURL!, error:NSError!) -> Void in print("\(path)") }) 

And "import ALAssetsLibrary" into your file.

Project-> Build Phases -> Binary Link -> AssetsLibrary.framework

+1
Feb 27 '16 at 15:13
source share
 - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)img editingInfo:(NSDictionary *)editInfo { RandomIndexnew = arc4random() % 3; if(RandomIndexnew == 0) { nameStr =[NSString stringWithFormat:@"jpg"]; textFieldNormalFile_type.text =[NSString stringWithFormat:@"jpg"]; } else if(RandomIndexnew = 1) { nameStr =[NSString stringWithFormat:@"gif"]; textFieldNormalFile_type.text =[NSString stringWithFormat:@"GIF"]; } else if(RandomIndexnew = 2) { nameStr =[NSString stringWithFormat:@"jpg"]; textFieldNormalFile_type.text =[NSString stringWithFormat:@"JPG"]; } RandomIndex = arc4random() % 20; NSString *nameStr1 =[NSString stringWithFormat:@"Image%i",RandomIndex]; textFieldNormalFile_name.text =[NSString stringWithFormat:@"%@.%@",nameStr1,nameStr]; newFilePath = [NSHomeDirectory() stringByAppendingPathComponent: textFieldNormalFile_name.text]; imageData = UIImageJPEGRepresentation(img, 1.0); if (imageData != nil) { NSLog(@"HERE [%@]", newFilePath); [imageData writeToFile:newFilePath atomically:YES]; } image.image =[UIImage imageNamed:newFilePath]; NSLog(@"newFilePath:%@",newFilePath); path.text =[NSString stringWithFormat:newFilePath]; NSLog(@"path.text :%@",path.text); } 
0
Dec 16 '10 at 7:55
source share

ALAssetsLibrary is deprecated.

So you have to do this:

 #import <Photos/Photos.h> UIImage *yourImage; [[PHPhotoLibrary sharedPhotoLibrary] performChanges:^{ [PHAssetChangeRequest creationRequestForAssetFromImage:yourImage]; } completionHandler:^(BOOL success, NSError *error) { if (success) { NSLog(@"Success"); } else { NSLog(@"write error : %@",error); } }]; 
0
Mar 09 '18 at 2:40
source share

OlivariesF answer is missing the key part of this question, get the path:

Here is a snippet of code that does everything:

 - (void)processImage:(UIImage*)image type:(NSString*)mimeType forCallbackId:(NSString*)callbackId { __block NSString* localId; // Add it to the photo library [[PHPhotoLibrary sharedPhotoLibrary] performChanges:^{ PHAssetChangeRequest *assetChangeRequest = [PHAssetChangeRequest creationRequestForAssetFromImage:image]; localId = [[assetChangeRequest placeholderForCreatedAsset] localIdentifier]; } completionHandler:^(BOOL success, NSError *err) { if (!success) { NSLog(@"Error saving image: %@", [err localizedDescription]); } else { PHFetchResult* assetResult = [PHAsset fetchAssetsWithLocalIdentifiers:@[localId] options:nil]; PHAsset *asset = [assetResult firstObject]; [[PHImageManager defaultManager] requestImageDataForAsset:asset options:nil resultHandler:^(NSData *imageData, NSString *dataUTI, UIImageOrientation orientation, NSDictionary *info) { NSURL *fileUrl = [info objectForKey:@"PHImageFileURLKey"]; if (fileUrl) { NSLog(@"Image path: %@", [fileUrl relativePath]); } else { NSLog(@"Error retrieving image filePath, heres whats available: %@", info); } }]; } }]; } 
0
Dec 05 '18 at 7:14
source share



All Articles