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

时间:2016-05-02 05:48:01

标签: ios objective-c iphone uiimagepickercontroller

我在我的应用程序中使用UIImagePickerController来获取图像。我需要在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;
}

3 个答案:

答案 0 :(得分:3)

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

答案 1 :(得分:0)

你可以这样做,

 -(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");
    }


}];

 }

并且不要忘记班上的@import Photos;

希望这会有所帮助:)

答案 2 :(得分:-1)

我使用与#MOHAMMAD ISHAQ 相同的代码,但在我的情况下不起作用,图片没有被删除,我没有任何错误。 请问有什么帮助或建议吗?

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

    }];

谢谢