简体   繁体   English

如何使用 UIImagePickerController 从照片库中删除图像

[英]How to delete an image from photo library using UIImagePickerController

I am using UIImagePickerController in my application to pick up the image.我在我的应用程序中使用UIImagePickerController来获取图像。 I need to delete this image synchronously from iOS PhotoLibrary after picking it up in my application.在我的应用程序中拾取后,我需要从 iOS PhotoLibrary 同步删除此图像。

- (BOOL)createAndInsertNewElementFromDictionary:(NSDictionary*)dict
{
AlbumElement *newElement;

if ([dict[UIImagePickerControllerMediaType]
     isEqualToString:(NSString*)kUTTypeMovie])
{
    NSURL *mediaUrl = dict[UIImagePickerControllerMediaURL];
    newElement = [AlbumElement createElementWithMediaUrl:mediaUrl
                                                 inAlbum:_album.name];

}
else if ([dict[UIImagePickerControllerMediaType]
          isEqualToString:(NSString*)kUTTypeImage])
{
    UIImage *image = [dict[UIImagePickerControllerOriginalImage] copy];
    newElement = [AlbumElement createElementWithImage:image
                                              inAlbum:_album.name];
}

if (newElement != nil)
{
    [_album.elements insertObject:newElement atIndex:0];

    UIImage *icon = [UIImage imageWithContentsOfFile:[newElement iconFullPath]];
    [AlbumElement writeImageToFileWithImage:icon
                                 atFullPath:_album.albumIconPath];
}
else
{
    NSLog(@"Element was NOT added!");
    return NO;
}

return YES;
}
NSURL *url = [dict objectForKey:@"UIImagePickerControllerReferenceURL"] ;  

PHPhotoLibrary *library = [PHPhotoLibrary sharedPhotoLibrary];
 [library performChanges:^{
// Here assetsURLs is array of url's you want to delete 
    PHFetchResult *assetsToBeDeleted = [PHAsset fetchAssetsWithALAssetURLs:[NSArray arrayWithObject:url] options:nil];
    [PHAssetChangeRequest deleteAssets:assetsToBeDeleted];
} completionHandler:^(BOOL success, NSError *error)
 {
     // Check error and success here
}];

You can do something like this, 你可以这样做,

 -(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info{


NSURL *imgURL = info[UIImagePickerControllerReferenceURL];

[[PHPhotoLibrary sharedPhotoLibrary]performChanges:^{


    PHAsset *imageAssetTodelete = [PHAsset fetchAssetsWithALAssetURLs:imgURL options:nil];

    [PHAssetChangeRequest deleteAssets:imageAssetTodelete];


} completionHandler:^(BOOL success, NSError * _Nullable error) {

    if (error) {

        NSLog(@"err description : %@",[error localizedDescription]);
    }
    if (success) {

        NSLog(@"image deleted successfully");
    }


}];

 }

And don't forget to @import Photos; 别忘了@import Photos; in your class. 在你的班上

Hope this will help :) 希望这会有所帮助:)

I use the same code like #MOHAMMAD ISHAQ but in my case that not work, the picture are not deleted, I have no any error.我使用与#MOHAMMAD ISHAQ 相同的代码,但在我的情况下不起作用,图片没有被删除,我没有任何错误。 Any help or suggestion please ?请问有什么帮助或建议吗?

PHAsset *asset = nil;
PHFetchOptions *fetchOptions = [[PHFetchOptions alloc] init];
fetchOptions.sortDescriptors = @[[NSSortDescriptor sortDescriptorWithKey:@"creationDate" ascending:YES]];
PHFetchResult *fetchResult = [PHAsset fetchAssetsWithMediaType:PHAssetMediaTypeImage options:fetchOptions];
if (fetchResult != nil && fetchResult.count > 0) {
    // get last photo from Photos
    asset = [fetchResult lastObject];
}

if (asset) {
    
    PHContentEditingInputRequestOptions *editOptions = [[PHContentEditingInputRequestOptions alloc] init];

    [asset requestContentEditingInputWithOptions:editOptions completionHandler:^(PHContentEditingInput *contentEditingInput, NSDictionary *info) {

        if (contentEditingInput.fullSizeImageURL) {
            //do something with contentEditingInput.fullSizeImageURL
            
            NSLog(@"¨PATH %@",contentEditingInput.fullSizeImageURL);
            
            NSMutableArray *persons = [[NSMutableArray alloc]initWithCapacity:0];
            [persons addObject:contentEditingInput.fullSizeImageURL];
            
            NSArray *myArray = [[NSArray alloc]initWithArray:persons];
            
            PHPhotoLibrary *library = [PHPhotoLibrary sharedPhotoLibrary];
          
            [library performChanges:^{
               
    
                PHFetchResult *assetsToBeDeleted = [PHAsset fetchAssetsWithALAssetURLs:myArray options:nil];
       
                [PHAssetChangeRequest deleteAssets:assetsToBeDeleted];
            } completionHandler:^(BOOL success, NSError *error)
             {
                 //do something here
                NSLog(@"DELETE IAMGE");
            }];
        }

    }];

Thank谢谢

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 swift:如何在不使用UIImagePickerController的情况下从照片库加载照片? - swift: How to load photos from photo library without using UIImagePickerController? 使用 UIImagePickerController 拍照后如何调整图像大小? - How to adjust a image size after using UIImagePickerController take a photo? 使用UIImagePickerController访问照片库后崩溃 - crash after accessing photo library using UIImagePickerController UIImagePickerController - 从照片库中挑选背景中的视频 - UIImagePickerController - picking video in background from photo library 使用UIImagePickerController从照片库中访问相机 - Accessing camera from within the photo library with UIImagePickerController UIImagePickerController 和照片库访问 - UIImagePickerController and photo library access 如何使用URL路径从iOS的照片库中获取图像? - How to fetch an image from photo library of iOS using URL path? 如何通过应用程序从设备的图片库中删除照片? - How to delete photo from the Photo's Library of device through the application? 我使用UIImagePickerController拾取图像后如何从PhotoLibrary中删除图像 - How to delete an image from PhotoLibrary after i pick it up using UIImagePickerController 如何使用 UIImagePickerController 设置拍摄照片的位置 - How to set the location for the captured photo by using UIImagePickerController
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM