如何在 PHAsset 中添加图像并将其从照片库中删除? [英] How to add image in PHAsset and delete it from photo library?

查看:101
本文介绍了如何在 PHAsset 中添加图像并将其从照片库中删除?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个类似隐藏照片的应用程序,我想将图像移动到我的应用程序中,因此我必须从照片库中导入图像并从库中删除该图像,但我无法理解如何使用 PHAssets 以及在何处实施.

I am working on an app like hiding photos and I want to move the images to my app so for that I have to import images from photo library and delete that image from library but I cant understand how to work with PHAssets and where to implement.

我使用 UIPickerview 选择图像,然后从库中删除它,请任何人都可以帮助我

I used the UIPickerview to pick the image and then delete it from library please anyone can help me for that

这是我获取图像的选择器:

UIImage *chosenImage = info[UIImagePickerControllerEditedImage];

这是删除但什么是资产:

[[PHPhotoLibrary sharedPhotoLibrary] performChanges:^{
    [PHAssetChangeRequest deleteAssets:formatWithOrientation];
} completionHandler:^(BOOL success, NSError *error) {
    NSLog(@"Finished deleting asset. %@", (success ? @"Success." : error));
}];

推荐答案

试试这个代码.它对我有用.

Try this code. It's working for me.

PHFetchResult *asset = [PHAsset fetchAssetsWithALAssetURLs:@"Your asset url" 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");  
    }
}];
}];

这篇关于如何在 PHAsset 中添加图像并将其从照片库中删除?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆