Delete Image from Custom Album PHPhotoLibrary objective C

1.2k views Asked by At

In my app I used edit pic and saved in custom folder in Gallery called "Fab". now is there anything to delete that image from folder? I have found different solution but they require asset URL. I used Photos framework so how to get asset url for particular image for deletion ?

2

There are 2 answers

4
Himanshu Moradiya On BEST ANSWER
 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.

1
Zღk On

Try this below code

ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];  

[library writeImageToSavedPhotosAlbum:[viewImage CGImage] orientation:(ALAssetOrientation)[viewImage imageOrientation] completionBlock:^(NSURL *assetURL, NSError *error){  
    if (error) {  
        NSLog(@"error");  
    } else {  
            NSLog(@"url %@", assetURL);  
    }  
}];  

will return url for saved image.