Search code examples
objective-cuipickerviewphotophotokitphasset

How to add image in PHAsset and delete it from photo library?


I am working on an app like hiding photos and I want to move the images to my app so for that I have to import images from photo library and delete that image from library but I cant understand how to work with PHAssets and where to implement.

I used the UIPickerview to pick the image and then delete it from library please anyone can help me for that

This the picker where I get image :

UIImage *chosenImage = info[UIImagePickerControllerEditedImage];

And this is for delete but what is asset :

[[PHPhotoLibrary sharedPhotoLibrary] performChanges:^{
    [PHAssetChangeRequest deleteAssets:formatWithOrientation];
} completionHandler:^(BOOL success, NSError *error) {
    NSLog(@"Finished deleting asset. %@", (success ? @"Success." : error));
}];

Solution

  • Try this code. It's working for me.

    PHFetchResult *asset = [PHAsset fetchAssetsWithALAssetURLs:@“Your asset url” options:nil];
    
     [asset enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
     NSLog(@"%@",[obj class]);
     [[PHPhotoLibrary sharedPhotoLibrary] performChanges:^{
         BOOL req = [obj canPerformEditOperation:PHAssetEditOperationDelete];
         if (req) {
             NSLog(@"true");
             [PHAssetChangeRequest deleteAssets:@[obj]];
         }
     } completionHandler:^(BOOL success, NSError *error) {
         NSLog(@"Finished Delete asset. %@", (success ? @"Success." : error));
         if (success) {
            NSLog(@“delete successfully”);  
        }
    }];
    }];