1

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;
}
Vinoth Krishnan
  • 2,925
  • 6
  • 29
  • 34
PPreeti
  • 201
  • 1
  • 3
  • 10

3 Answers3

3
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
}];
MOHAMMAD ISHAQ
  • 988
  • 7
  • 15
  • Hi, thanks it works fine for me. But it showing message when deleting photo . How to modify that message. – PPreeti May 02 '16 at 06:30
  • It is confirmation message , By default from apple in this approach you can't hide this. I will share if i find another approach . For more info check this link http://stackoverflow.com/questions/31922796/disable-confirmation-on-delete-request-in-phphotolibrary – MOHAMMAD ISHAQ May 02 '16 at 06:31
  • Is it possible to select multiple Images from photo library. – PPreeti May 02 '16 at 06:37
  • No from default , But you can impalement it via 3rd party libraries as https://github.com/w5mith/WSAssetPickerController – MOHAMMAD ISHAQ May 02 '16 at 06:39
  • if you use camera to capture image , this will not save in photo library . – MOHAMMAD ISHAQ May 02 '16 at 06:43
0

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 :)

Ketan Parmar
  • 27,092
  • 9
  • 50
  • 75
-1

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