简体   繁体   中英

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));
}];

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”);  
    }
}];
}];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM