How to get the URL of an image just added in PHPhotoLibrary - url

I am using the UIImagePickerController in two cases
to select an existing image in the Photo Library
to take a new picture
In the first case, when I choose an image form the library, I can easily get the URL in the delegate method:
- (void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
// Get the URL
NSURL *url = [info valueForKey:UIImagePickerControllerReferenceURL];
...
}
But when I take a new picture, the image is not yet in the photo library and has no URL yet. So, I first need to add the image in the Library. But then, how to get the URL of the new asset?
Here is my code to add the image in the Photo Library
- (void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
// Get the image
UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];
// Add the image in the library
[[PHPhotoLibrary sharedPhotoLibrary]
performChanges:^
{
// Request creating an asset from the image.
PHAssetChangeRequest *createAssetRequest = [PHAssetChangeRequest creationRequestForAssetFromImage:image];
// Get the URL of the new asset here ?
...
}
completionHandler:^(BOOL success, NSError *error)
{
if (!success) { ...; return; }
// Get the URL of the new asset here ?
...
}
];
}

I didn't find the way to get URL, but maybe localIdentifier can help you do the same work.
use
NSString* localId = [[assetChangeRequest placeholderForCreatedAsset] localIdentifier];
to get the local ID and get the image later.
__block NSString* localId;
// Add it to the photo library
[[PHPhotoLibrary sharedPhotoLibrary] performChanges:^{
PHAssetChangeRequest *assetChangeRequest = [PHAssetChangeRequest creationRequestForAssetFromImage:image];
if (self.assetCollection) {
PHAssetCollectionChangeRequest *assetCollectionChangeRequest = [PHAssetCollectionChangeRequest changeRequestForAssetCollection:self.assetCollection];
[assetCollectionChangeRequest addAssets:#[[assetChangeRequest placeholderForCreatedAsset]]];
}
localId = [[assetChangeRequest placeholderForCreatedAsset] localIdentifier];
} completionHandler:^(BOOL success, NSError *error) {
PHFetchResult* assetResult = [PHAsset fetchAssetsWithLocalIdentifiers:#[localId] options:nil];
if (!success) {
NSLog(#"Error creating asset: %#", error);
} 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) {
UIImage* newImage = [UIImage imageWithData:imageData];
self.imageView.image = newImage;
}];
}
}];

If we use the ALAssetsLibrary, it is very simple to save a picture in the Photo Album and get its URL:
// Get the image
UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];
// Add the image in the Photo Library
ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
[library writeImageToSavedPhotosAlbum: [image CGImage]
orientation: (ALAssetOrientation)[image imageOrientation]
completionBlock: ^(NSURL *assetURL, NSError *error)
{
if (error)
{
NSLog( #"error: %#", error );
}
else
{
NSLog( #"assetURL = %#", assetURL );
}
}];
But surprisingly it seems not possible to do the same thing with the new PHPhotoLibrary!
A solution to have the same result with PHPhotoLibrary is always welcome.

I don't like this fix, as this could result in a race condition. So far I can't think of a better solution. If someone does I'd love to hear it :)
Either way, here is a Swift-version of Rigel Chen's answer
import Photos
func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {
if let image = info[UIImagePickerControllerOriginalImage] as? UIImage {
var localId:String?
let imageManager = PHPhotoLibrary.sharedPhotoLibrary()
imageManager.performChanges({ () -> Void in
let request = PHAssetChangeRequest.creationRequestForAssetFromImage(image)
localId = request.placeholderForCreatedAsset?.localIdentifier
}, completionHandler: { (success, error) -> Void in
dispatch_async(dispatch_get_main_queue(), { () -> Void in
if let localId = localId {
let result = PHAsset.fetchAssetsWithLocalIdentifiers([localId], options: nil)
let assets = result.objectsAtIndexes(NSIndexSet(indexesInRange: NSRange(location: 0, length: result.count))) as? [PHAsset] ?? []
if let asset = assets.first {
// Do something with result
}
}
})
})
}
}

Swift 4 solution that works for me to get the url of last image in Photo Library:
let fetchOptions = PHFetchOptions()
fetchOptions.sortDescriptors = [NSSortDescriptor(key: "creationDate", ascending: true)]
let fetchResult = PHAsset.fetchAssets(with: .image, options: fetchOptions).lastObject
PHImageManager().requestAVAsset(forVideo: fetchResult!, options: nil, resultHandler { (avurlAsset, _, _) in
if let newObj = avurlAsset as? AVURLAsset {
print(newObj.url)
}
})
Hope it helps!

[Swift 4] This method can handle both image and video, enjoy :)
func getURL(of asset: PHAsset, completionHandler : #escaping ((_ responseURL : URL?) -> Void)) {
if asset.mediaType == .image {
let options: PHContentEditingInputRequestOptions = PHContentEditingInputRequestOptions()
options.canHandleAdjustmentData = {(adjustmeta: PHAdjustmentData) -> Bool in
return true
}
asset.requestContentEditingInput(with: options, completionHandler: { (contentEditingInput, info) in
completionHandler(contentEditingInput!.fullSizeImageURL)
})
} else if asset.mediaType == .video {
let options: PHVideoRequestOptions = PHVideoRequestOptions()
options.version = .original
PHImageManager.default().requestAVAsset(forVideo: asset, options: options, resultHandler: { (asset, audioMix, info) in
if let urlAsset = asset as? AVURLAsset {
let localVideoUrl = urlAsset.url
completionHandler(localVideoUrl)
} else {
completionHandler(nil)
}
})
}
}

Retrieve image URL:
- (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);
}
}];
}
}];
}

Related

How to get the geolocation of video chosed through uiimagepickercontroller

I am trying to get the geolocation of the image and video though PHAssets, image location is being fetched through.
PHAsset.fetchAssetsWithALAssetURLs([imageUrl], options: opts).
When i used the same below code for video this returned zero.
PHAsset.fetchAssetsWithALAssetURLs([videoUrl], options: opts)
This is tested on iOS 8 and works for videos so it should work similarly for photos with a few tweaks.
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
NSURL *videoUrl = (NSURL *)[info objectForKey:UIImagePickerControllerMediaURL];
NSString *moviePath = [videoUrl path];
if ( UIVideoAtPathIsCompatibleWithSavedPhotosAlbum(moviePath) ) {
ALAssetsLibrary *assetLibrary = [[ALAssetsLibrary alloc] init];
[assetLibrary assetForURL:[info objectForKey:UIImagePickerControllerReferenceURL] resultBlock:^(ALAsset *asset) {
CLLocation *location = [asset valueForProperty:ALAssetPropertyLocation];
NSLog(#"Location Meta: %#", location);
} failureBlock:^(NSError *error) {
NSLog(#"Video Date Error: %#", error);
}];
}
}
Get all videos Gelocation
PHPhotoLibrary.requestAuthorization { (status) -> Void in
let videosOption = PHFetchOptions()
videosOption.predicate = NSPredicate(format: "mediaType = %d", PHAssetMediaType.video.rawValue)
videosOption.sortDescriptors = [NSSortDescriptor(key: "creationDate", ascending: true)]
let allVids = PHAsset.fetchAssets(with: videosOption)
for index in 0..<allVids.count {
//print Location here
print(allVids[index].location as Any)
}
}

FetchAssetsWithLocalIdentifiers returns empty array

I'm using Photos.Framework to save photos taken from the camera into my gallery and to retrieve them.
This is the code I'm using to store the photos:
__block PHAssetCollection *album = [self getMyAlbumWithName:#"MyAlbumName"];
if(album == nil)
{
[self makeAlbumWithTitle:#"MyAlbumName" onSuccess:^(NSString *AlbumId) {
album = [self getMyAlbumWithName:#"MyAlbumName"];
[self addNewAssetWithImage:_imageToStore toAlbum:album onSuccess:^(NSString *ImageId)
{
_imageLocalIdentifier = imageId;
} onError:^(NSError *error) {
// No need to do anything
}];
} onError:^(NSError *error) {
// No need to do anything
}];
}
else
{
[self addNewAssetWithImage:_imageToStore toAlbum:album onSuccess:^(NSString *ImageId)
{
_imageLocalIdentifier = imageId;
} onError:^(NSError *error) {
// No need to do anything
}];
}
-(PHAssetCollection *)getMyAlbumWithName:(NSString*)AlbumName
{
PHFetchResult *assetCollections = [PHAssetCollection fetchAssetCollectionsWithType:PHAssetCollectionTypeAlbum
subtype:PHAssetCollectionSubtypeAlbumRegular
options:nil];
NSLog(#"assetCollections.count = %lu", assetCollections.count);
if (assetCollections.count == 0) return nil;
__block PHAssetCollection * myAlbum;
[assetCollections enumerateObjectsUsingBlock:^(PHAssetCollection *album, NSUInteger idx, BOOL *stop) {
NSLog(#"album:%#", album);
NSLog(#"album.localizedTitle:%#", album.localizedTitle);
if ([album.localizedTitle isEqualToString:AlbumName]) {
myAlbum = album;
*stop = YES;
}
}];
if (!myAlbum) return nil;
return myAlbum;
}
-(void)makeAlbumWithTitle:(NSString *)title onSuccess:(void(^)(NSString *AlbumId))onSuccess onError: (void(^)(NSError * error)) onError
{
//Check weather the album already exist or not
if (![self getMyAlbumWithName:title])
{
[[PHPhotoLibrary sharedPhotoLibrary] performChanges:^{
// Request editing the album.
PHAssetCollectionChangeRequest *createAlbumRequest = [PHAssetCollectionChangeRequest creationRequestForAssetCollectionWithTitle:title];
// Get a placeholder for the new asset and add it to the album editing request.
PHObjectPlaceholder * placeHolder = [createAlbumRequest placeholderForCreatedAssetCollection];
if (placeHolder)
{
onSuccess(placeHolder.localIdentifier);
}
} completionHandler:^(BOOL success, NSError *error) {
NSLog(#"Finished adding asset. %#", (success ? #"Success" : error));
if (error)
{
onError(error);
}
}];
}
}
-(void)addNewAssetWithImage:(UIImage *)image
toAlbum:(PHAssetCollection *)album
onSuccess:(void(^)(NSString *ImageId))onSuccess
onError: (void(^)(NSError * error)) onError
{
[[PHPhotoLibrary sharedPhotoLibrary] performChanges:^{
// Request creating an asset from the image.
PHAssetChangeRequest *createAssetRequest = [PHAssetChangeRequest creationRequestForAssetFromImage:image];
// Request editing the album.
PHAssetCollectionChangeRequest *albumChangeRequest = [PHAssetCollectionChangeRequest changeRequestForAssetCollection:album];
// Get a placeholder for the new asset and add it to the album editing request.
PHObjectPlaceholder * placeHolder = [createAssetRequest placeholderForCreatedAsset];
[albumChangeRequest addAssets:#[ placeHolder ]];
NSLog(#"%#",placeHolder.localIdentifier);
if (placeHolder) {
onSuccess(placeHolder.localIdentifier);
}
} completionHandler:^(BOOL success, NSError *error) {
NSLog(#"Finished adding asset. %#", (success ? #"Success" : error));
if (error) {
onError(error);
}
}];
}
And this is the code I'm using to retrieve this photo:
PHImageManager *imgManager = [[PHImageManager alloc] init];
PHFetchResult* fetchResult = [PHAsset fetchAssetsWithLocalIdentifiers:#[_imageLocalIdentifier] options:nil];
if([fetchResult count] > 0)
{
PHAsset *asset = [fetchResult objectAtIndex:0];
PHImageRequestOptions *option = [PHImageRequestOptions new];
option.synchronous = NO;
option.version = PHImageRequestOptionsVersionCurrent;
option.networkAccessAllowed = YES;
option.deliveryMode = PHImageRequestOptionsDeliveryModeOpportunistic;
option.resizeMode = PHImageRequestOptionsResizeModeFast;
[imgManager requestImageForAsset:asset
targetSize:CGSizeMake(CAMERA_GALLERY_SIZE, CAMERA_GALLERY_SIZE)
contentMode:PHImageContentModeDefault
options:option
resultHandler:^(UIImage *result, NSDictionary *info) {
[cell.photoIV setImage:result];
}];
}
With this piece of code, over a sample of 12 photos stored (they are ok in my album) 4 or 5 of their localidentifiers returns an empty fetch results.
This is tested in iOS 8, iOS 9 and iOS 10 (with iOS 10 it's indeed worse because almost all of the fetch results are empty).
I've read that something similar to this was a bug in previous versions of iOS, but I guess this is not the reason now.
I've tried with this method to retrieve the photos:
- (PHAsset *)getAssetFromGallery:(NSString *)identifier
{
PHAsset *asset = [PHAsset fetchAssetsWithLocalIdentifiers:#[identifier] options:nil].lastObject;
if(asset != nil)
return asset;
__block PHAsset *result;
PHFetchResult *userAlbums = [PHAssetCollection fetchAssetCollectionsWithType:PHAssetCollectionTypeAlbum subtype:PHAssetCollectionSubtypeAny options:nil];
PHFetchOptions *fetchOptions = [[PHFetchOptions alloc] init];
[fetchOptions setPredicate:[NSPredicate predicateWithFormat:#"localIdentifier == %#", identifier]];
[userAlbums enumerateObjectsUsingBlock:^(id _Nonnull objectCollection, NSUInteger idx, BOOL * _Nonnull stopCollectionEnumeration) {
PHAssetCollection *collection = nil;
if(![objectCollection isKindOfClass:[PHAssetCollection class]])
return;
collection = (PHAssetCollection *)objectCollection;
PHFetchResult *assetsFetchResult = [PHAsset fetchAssetsInAssetCollection:collection options:fetchOptions];
[assetsFetchResult enumerateObjectsUsingBlock:^(id _Nonnull objectAsset, NSUInteger idx, BOOL * _Nonnull stopAssetEnumeration) {
PHAsset *asset = nil;
if(![objectAsset isKindOfClass:[PHAsset class]])
return;
result = asset;
*stopAssetEnumeration = YES;
*stopCollectionEnumeration = YES;
}];
}];
return asset;
}
I've tried with PHAssetCollectionSubtypeAlbumMyPhotoStream instead of PHAssetCollectionSubtypeAny.
And I've tried with #"localIdentifier ==[cd] %#" instead of #"localIdentifier == %#".
And always the same results, lots of times the fetch results is empty.
Any idea of what is it happening?
My problem was that I wasn't saving the photos in the right way, I was calling onSuccess(placeHolder.localIdentifier); inside the performChanges block instead of inside the completionHandler block.
This is the code I'm using now to save the photos:
__block PHAssetCollection *album = [AuxiliaryFunctions getMyAlbumWithName:#"MyAlbumName" orWithIdentifier:#""];
if(album == nil)
[self makeAlbumWithTitle:#"MyAlbumName" onSuccess:^(NSString *AlbumId) {
album = [self getMyAlbumWithName:#"MyAlbumName" orWithIdentifier:AlbumId];
[self addNewAssetWithImage:_imageToStore toAlbum:album onSuccess:^(NSString *ImageId)
{
_imageLocalIdentifier = imageId;
} onError:^(NSError *error) {
// No need to do anything
}];
} onError:^(NSError *error) {
// No need to do anything
}];
else
{
[self addNewAssetWithImage:_imageToStore toAlbum:album onSuccess:^(NSString *ImageId)
{
_imageLocalIdentifier = imageId;
} onError:^(NSError *error) {
// No need to do anything
}];
}
-(PHAssetCollection *)getMyAlbumWithName:(NSString*)AlbumName orWithIdentifier:(NSString *)identifier
{
PHFetchResult *assetCollections = nil;
if(![identifier isEqualToString:#""])
{
PHFetchOptions *options = [PHFetchOptions new];
options.predicate = [NSPredicate predicateWithFormat:#"localIdentifier = %# OR title = %#", identifier, AlbumName];
assetCollections = [PHAssetCollection fetchAssetCollectionsWithLocalIdentifiers:#[identifier]
options:options];
}
else
{
PHFetchOptions *options = [PHFetchOptions new];
options.predicate = [NSPredicate predicateWithFormat:#"title = %#", AlbumName];
assetCollections = [PHAssetCollection fetchAssetCollectionsWithType:PHAssetCollectionTypeAlbum
subtype:PHAssetCollectionSubtypeAny
options:options];
}
NSLog(#"assetCollections.count = %lu", assetCollections.count);
if (assetCollections.count == 0) return nil;
__block PHAssetCollection * myAlbum;
[assetCollections enumerateObjectsUsingBlock:^(PHAssetCollection *album, NSUInteger idx, BOOL *stop) {
NSLog(#"album:%#", album);
NSLog(#"album.localizedTitle:%#", album.localizedTitle);
if ([album.localizedTitle isEqualToString:AlbumName]) {
myAlbum = album;
*stop = YES;
}
}];
if (!myAlbum) return nil;
return myAlbum;
}
-(void)makeAlbumWithTitle:(NSString *)title onSuccess:(void(^)(NSString *AlbumId))onSuccess onError: (void(^)(NSError * error)) onError
{
__block NSString *localIdentifier = #"";
//Check weather the album already exist or not
if (![self getMyAlbumWithName:title orWithIdentifier:#""])
{
[[PHPhotoLibrary sharedPhotoLibrary] performChanges:^{
// Request editing the album.
PHAssetCollectionChangeRequest *createAlbumRequest = [PHAssetCollectionChangeRequest creationRequestForAssetCollectionWithTitle:title];
// Get a placeholder for the new asset and add it to the album editing request.
PHObjectPlaceholder * placeHolder = [createAlbumRequest placeholderForCreatedAssetCollection];
if (placeHolder)
{
localIdentifier = placeHolder.localIdentifier;
// This line was the problem
//onSuccess(localIdentifier);
}
} completionHandler:^(BOOL success, NSError *error) {
NSLog(#"Finished adding asset. %#", (success ? #"Success" : error));
if(success)
{
onSuccess(localIdentifier);
}
if (error)
{
onError(error);
}
}];
}
}
-(void)addNewAssetWithImage:(UIImage *)image
toAlbum:(PHAssetCollection *)album
onSuccess:(void(^)(NSString *ImageId))onSuccess
onError: (void(^)(NSError * error)) onError
{
__block NSString *localIdentifier = #"";
[[PHPhotoLibrary sharedPhotoLibrary] performChanges:^{
// Request creating an asset from the image.
PHAssetChangeRequest *createAssetRequest = [PHAssetChangeRequest creationRequestForAssetFromImage:image];
// Request editing the album.
PHAssetCollectionChangeRequest *albumChangeRequest = [PHAssetCollectionChangeRequest changeRequestForAssetCollection:album];
// Get a placeholder for the new asset and add it to the album editing request.
PHObjectPlaceholder * placeHolder = [createAssetRequest placeholderForCreatedAsset];
[albumChangeRequest addAssets:#[ placeHolder ]];
NSLog(#"%#",placeHolder.localIdentifier);
if (placeHolder) {
localIdentifier = placeHolder.localIdentifier;
// This line was the problem
//onSuccess(localIdentifier);
}
} completionHandler:^(BOOL success, NSError *error) {
NSLog(#"Finished adding asset. %#", (success ? #"Success" : error));
if(success)
{
onSuccess(localIdentifier);
}
if (error)
{
onError(error);
}
}];
}

How to delete an image from photo library using UIImagePickerController

I am using UIImagePickerController in my application to pick up the image. I need to delete this image synchronously from iOS PhotoLibrary after picking it up in my application.
- (BOOL)createAndInsertNewElementFromDictionary:(NSDictionary*)dict
{
AlbumElement *newElement;
if ([dict[UIImagePickerControllerMediaType]
isEqualToString:(NSString*)kUTTypeMovie])
{
NSURL *mediaUrl = dict[UIImagePickerControllerMediaURL];
newElement = [AlbumElement createElementWithMediaUrl:mediaUrl
inAlbum:_album.name];
}
else if ([dict[UIImagePickerControllerMediaType]
isEqualToString:(NSString*)kUTTypeImage])
{
UIImage *image = [dict[UIImagePickerControllerOriginalImage] copy];
newElement = [AlbumElement createElementWithImage:image
inAlbum:_album.name];
}
if (newElement != nil)
{
[_album.elements insertObject:newElement atIndex:0];
UIImage *icon = [UIImage imageWithContentsOfFile:[newElement iconFullPath]];
[AlbumElement writeImageToFileWithImage:icon
atFullPath:_album.albumIconPath];
}
else
{
NSLog(#"Element was NOT added!");
return NO;
}
return YES;
}
NSURL *url = [dict objectForKey:#"UIImagePickerControllerReferenceURL"] ;
PHPhotoLibrary *library = [PHPhotoLibrary sharedPhotoLibrary];
[library performChanges:^{
// Here assetsURLs is array of url's you want to delete
PHFetchResult *assetsToBeDeleted = [PHAsset fetchAssetsWithALAssetURLs:[NSArray arrayWithObject:url] options:nil];
[PHAssetChangeRequest deleteAssets:assetsToBeDeleted];
} completionHandler:^(BOOL success, NSError *error)
{
// Check error and success here
}];
You can do something like this,
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info{
NSURL *imgURL = info[UIImagePickerControllerReferenceURL];
[[PHPhotoLibrary sharedPhotoLibrary]performChanges:^{
PHAsset *imageAssetTodelete = [PHAsset fetchAssetsWithALAssetURLs:imgURL options:nil];
[PHAssetChangeRequest deleteAssets:imageAssetTodelete];
} completionHandler:^(BOOL success, NSError * _Nullable error) {
if (error) {
NSLog(#"err description : %#",[error localizedDescription]);
}
if (success) {
NSLog(#"image deleted successfully");
}
}];
}
And don't forget to #import Photos; in your class.
Hope this will help :)
I use the same code like #MOHAMMAD ISHAQ but in my case that not work, the picture are not deleted, I have no any error.
Any help or suggestion please ?
PHAsset *asset = nil;
PHFetchOptions *fetchOptions = [[PHFetchOptions alloc] init];
fetchOptions.sortDescriptors = #[[NSSortDescriptor sortDescriptorWithKey:#"creationDate" ascending:YES]];
PHFetchResult *fetchResult = [PHAsset fetchAssetsWithMediaType:PHAssetMediaTypeImage options:fetchOptions];
if (fetchResult != nil && fetchResult.count > 0) {
// get last photo from Photos
asset = [fetchResult lastObject];
}
if (asset) {
PHContentEditingInputRequestOptions *editOptions = [[PHContentEditingInputRequestOptions alloc] init];
[asset requestContentEditingInputWithOptions:editOptions completionHandler:^(PHContentEditingInput *contentEditingInput, NSDictionary *info) {
if (contentEditingInput.fullSizeImageURL) {
//do something with contentEditingInput.fullSizeImageURL
NSLog(#"¨PATH %#",contentEditingInput.fullSizeImageURL);
NSMutableArray *persons = [[NSMutableArray alloc]initWithCapacity:0];
[persons addObject:contentEditingInput.fullSizeImageURL];
NSArray *myArray = [[NSArray alloc]initWithArray:persons];
PHPhotoLibrary *library = [PHPhotoLibrary sharedPhotoLibrary];
[library performChanges:^{
PHFetchResult *assetsToBeDeleted = [PHAsset fetchAssetsWithALAssetURLs:myArray options:nil];
[PHAssetChangeRequest deleteAssets:assetsToBeDeleted];
} completionHandler:^(BOOL success, NSError *error)
{
//do something here
NSLog(#"DELETE IAMGE");
}];
}
}];
Thank

Is there a way to save a Live Photo to the Photo Library?

I'm passing stored image and video files to:
PHLivePhoto.requestLivePhotoWithResourceFileURLs and getting a PHLivePhoto object that I can display in PHLivePhotoView. But I am wondering, once I have a PHLivePhoto is there a way to save it to the Photo Library?
NSURL *photoURL = ...;
NSURL *videoURL = ...;
[[PHPhotoLibrary sharedPhotoLibrary] performChanges:^{
PHAssetCreationRequest *request = [PHAssetCreationRequest creationRequestForAsset];
//These types should be inferred from your files
//PHAssetResourceCreationOptions *photoOptions = [[PHAssetResourceCreationOptions alloc] init];
//photoOptions.uniformTypeIdentifier = #"public.jpeg";
//PHAssetResourceCreationOptions *videoOptions = [[PHAssetResourceCreationOptions alloc] init];
//videoOptions.uniformTypeIdentifier = #"com.apple.quicktime-movie";
[request addResourceWithType:PHAssetResourceTypePhoto fileURL:photoURL options:nil /*photoOptions*/];
[request addResourceWithType:PHAssetResourceTypePairedVideo fileURL:videoURL options:nil /*videoOptions*/];
} completionHandler:^(BOOL success, NSError * _Nullable error) {
NSLog(#"success? %d, error: %#",success,error);
}];
Swift 3:
PHPhotoLibrary.shared().performChanges({
let request = PHAssetCreationRequest.forAsset()
request.addResource(with: .photo, fileURL: photoURL, options: nil)
request.addResource(with: .pairedVideo, fileURL: videoURL, options: nil)
}) { (success, error) in
print(success)
print(error?.localizedDescription ?? "error")
}

Save image to photo library using photo framework

My app crashes every time when I try to save image using photo framework.
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{
_mChangeRequest = [PHAssetChangeRequest creationRequestForAssetFromImage:[info valueForKey:UIImagePickerControllerOriginalImage]];
[[PHPhotoLibrary sharedPhotoLibrary] performChanges:^{
_mChangeRequest = [PHAssetChangeRequest creationRequestForAssetFromImage:[info valueForKey:UIImagePickerControllerOriginalImage]];
} completionHandler:^(BOOL success, NSError *error) {
if (success) {
PHObjectPlaceholder *assetPlaceholder = _mChangeRequest.placeholderForCreatedAsset;
}
else {
NSLog(#"write error : %#",error);
}
}];
}
crash : NSInternalInconsistencyException', reason: 'This method can only be called from inside of -[PHPhotoLibrary performChanges:completionHandler:] or -[PHPhotoLibrary performChangesAndWait:error:]'
All you need to do is trigger a creation request. As the error says, you can access the change request only inside the performChanges block.
So to save the image you would do something like this:
[[PHPhotoLibrary sharedPhotoLibrary] performChanges:^{
[PHAssetChangeRequest creationRequestForAssetFromImage:[info valueForKey:UIImagePickerControllerOriginalImage]];
} completionHandler:^(BOOL success, NSError *error) {
if (success) {
NSLog(#"Success");
}
else {
NSLog(#"write error : %#",error);
}
}];
In case you need to do something with the placeholder of the newly created asset, you can access it inside the same performChanges block:
PHAssetChangeRequest *changeRequest = [PHAssetChangeRequest creationRequestForAssetFromImage:[info valueForKey:UIImagePickerControllerOriginalImage]];
PHObjectPlaceholder *assetPlaceholder = changeRequest.placeholderForCreatedAsset;
In Swift 3 I do this to save the video to the library.
if mediaType.isEqual(to: (kUTTypeMovie as NSString) as String) {
if let videoURL = info[UIImagePickerControllerMediaURL] as? URL {
PHPhotoLibrary.shared().performChanges({
_ = PHAssetChangeRequest.creationRequestForAssetFromVideo(atFileURL: videoURL)
}, completionHandler: { (success, error) in
if success {
print("ok")
let videoData = NSData(contentsOf: videoURL)
// use videoData here if needed...
if let posterImage = self.firstFrame(videoURL: videoURL) {
self.imageView.image = posterImage
}
picker.dismiss(animated: true) { () -> Void in
}
} else {
print(error?.localizedDescription)
}
})
}
}
Here is an example of code how you can write/save image to Photo Library using UIImageWriteToSavedPhotosAlbum function:
- (void)saveImage:(UIImage *)image {
UIImageWriteToSavedPhotosAlbum(self.image, self, #selector(image:didFinishSavingWithError:contextInfo:), NULL);
}
- (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo;
{
if (!error) {
// saved successfully
PHFetchOptions *fetchOptions = [PHFetchOptions new];
fetchOptions.sortDescriptors = #[[NSSortDescriptor sortDescriptorWithKey:#"creationDate" ascending:NO]];
PHAsset *asset = [PHAsset fetchAssetsWithMediaType:PHAssetMediaTypeImage options:fetchOptions].firstObject;
if (asset != nil) {
// here you can use asset of your image
}
} else {
NSLog(#"save image error: %#", error);
}
}
Don't forget to add into your Info.plist a key-value pair Privacy - Camera Usage Description with description of usage.
delete third line of the code
Verify that the _mChangeRequest is __block variable
compile and run
you will see that image in the photos app
you will change code probably like this...
- (void)imagePickerController:(UIImagePickerController *)picker
didFinishPickingMediaWithInfo:(NSDictionary *)info {
__block PHAssetChangeRequest *_mChangeRequest = nil;
[[PHPhotoLibrary sharedPhotoLibrary]
performChanges:^{
UIImage *image = [info valueForKey:UIImagePickerControllerOriginalImage];
_mChangeRequest = [PHAssetChangeRequest creationRequestForAssetFromImage:image];
}, completionHandler :^(BOOL success, NSError *error) {
if (success) {
PHObjectPlaceholder *assetPlaceholder = _mChangeRequest.placeholderForCreatedAsset;
} else {
NSLog(#"write error : %#", error);
}
}];
}

Resources