简体   繁体   English

如何在PHAsset中添加图像并将其从照片库中删除?

[英]How to add image in PHAsset and delete it from photo library?

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. 我正在开发一个类似隐藏照片的应用程序,我想将图像移到我的应用程序中,因此我必须从照片库导入图像,然后从库中删除该图像,但是我不明白如何使用PHAssets以及在哪里实现。

I used the UIPickerview to pick the image and then delete it from library please anyone can help me for that 我使用了UIPickerview来选择图像,然后将其从库中删除,任何人都可以帮助我

This the picker where I get image : 这是我得到图像的选择器:

UIImage *chosenImage = info[UIImagePickerControllerEditedImage];

And this is for delete but what is asset : 这是用于删除,但资产是什么:

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

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

相关问题 如何使用 UIImagePickerController 从照片库中删除图像 - How to delete an image from photo library using UIImagePickerController 如何将照片库或相机中的图像添加到我的Ipad应用程序? - how to add image from photo library or camera to my Ipad application? 我想以编程方式从照片库中删除图像? - I want to delete image from photo library programmatically? 如何在IOS 9上使用UIImagePicker结果URL和PHAsset从媒体库加载图像 - How can i load image from Media Library using UIImagePicker results URL and PHAsset on IOS 9 从用户的照片库中删除照片? - Delete a photo from the user's photo library? 如何从PHAsset获取图像URL? 是否可以使用PHAsset URL保存图像以记录目录? - How To get Image URL From PHAsset? Is it possible To save Image using PHAsset URL to document Directory? 如何从 iOS SDK 中的设备(“照片”)中永久删除 PHAsset? - How to delete PHAsset permanently from device(“Photos”) in iOS SDK? 如何检测照片库中的图像是由iPhone相机拍摄还是导入 - how to detect if image from photo library was taken by iPhone camera or imported 从PHAsset获取缩略图图像 - Get Thumbnail Image from PHAsset 如何从PHAsset获取原始图像和媒体类型? - How to get Original Image and media type from PHAsset?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM