Deleting a Camera Roll Asset Using Photos Framework

Deleting photo from Photo Library using Photo framework

The clue is in the name ‘assets’ plural - the API wants an array or any other collection type that conforms to NSFastEnumeration e.g Set

PHAssetChangeRequest.deleteAssets([assetToDelete] as NSArray)

https://developer.apple.com/documentation/photos/phassetchangerequest/1624062-deleteassets

Delete an asset (picture or video) from IPhone in IOS

Possible duplication of https://stackoverflow.com/a/11058934/300292. Simple answer: you can't. The Photos app is the only place you can delete assets. Which is probably a good thing--you wouldn't want any willy-nilly app to be able to delete all your photos, would you?

How to delete an image from PhotoLibrary after i pick it up using UIImagePickerController

Just to add to the above,
For swift 3.0 this worked for me.

PHPhotoLibrary.shared().performChanges({
let imageAssetToDelete = PHAsset.fetchAssets(withALAssetURLs: imageUrls as! [URL], options: nil)
PHAssetChangeRequest.deleteAssets(imageAssetToDelete)
}, completionHandler: {success, error in
print(success ? "Success" : error )
})

Delete Image from Custom Album PHPhotoLibrary objective C

 PHAsset *tempPhasset = [_arrImageForAssetCameraRoll objectAtIndex:index]; // here pass your PHasset that you want to delete .
NSString *localStr=tempPhasset.localIdentifier;
NSRange range = [localStr rangeOfString:@"/"];
NSString *newString = [localStr substringToIndex:range.location];
NSString *appendedString=[NSString stringWithFormat:@"%@%@%@",@"assets-library://asset/asset.JPG?id=",newString,@"&ext=JPG"];
NSLog(@"%@ phasset ",appendedString);
NSURL *deleteurl = [NSURL URLWithString:appendedString];
NSArray *arrDelete = [[NSArray alloc] initWithObjects:deleteurl , nil];
PHFetchResult *asset = [PHAsset fetchAssetsWithALAssetURLs:arrDelete 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");
}else{
NSLog(@"delete Cancel");

}
}];

Any query about my code then put comment .
Happy Coding.



Related Topics



Leave a reply



Submit