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
How to Create Public Extensions, in a Shared Framework, for Xctest
Problems Accessing Calendar Using Ekeventstore on Osx Sierra with Swift 3
Calling Nsexception.Raise() in Swift
How to Make Swiftui Uiviewrepresentable View Hug Its Content
Closures Return Value (Previously Completionblock)
How to Add Custom Init for String Extension
Need Detailed Explanation for Memoize Implementation in Swift (Wwdc 14, Session 404)
How to Get the Unicode Codepoint Represented by an Integer in Swift
Nsurlsessiondatadelegate Method Didreceivedata and Others Are Not Called
How to I Turn Off the Ambient Light in Scene Kit (With Swift)
Different Colors for Bars in Barchart Depend on Value
What Does a "Do Statement" Without Catch Block Mean
Swiftui Tabbar: Action for Tapping Tabitem of Currently Selected Tab to Reset View
How to Add Floating Button on Top of the Uitableview
Create a Swift Dictionary Subclass