IOS Delete photos from the album-from Pinterest

Source: Internet
Author: User

From: HTTP://WWW.JIANSHU.COM/P/AC18AA3F28C2

Recently, the company's app has a new feature is to delete photos of the album in the app, originally a relatively simple function, in the process found that the Assetslibary framework does not provide the removal of album API, the network to find the time this information is very small, Finally found a clever method on the StackOverflow, using [Alasset SetImageData:metadata:completionBlock:] To achieve the function of deleting photos, but others say that although the API can delete photos, However, its main function is not to delete the photos, it is invalid to delete the photos only when iOS8. Fortunately, in iOS8 we can use the Photokit framework directly to complete the operation, Photokit directly provides the [Phassetchangerequest Deleteassets:@[asset]]api to complete the deletion of the photo function. Let's take a look at how the two photo gallery frameworks Delete photos.

In the previous iOS app we used the Assetslibary framework to do the photo gallery, but as the iOS system was updated, there was a big change in the camera and photo gallery, such as the new iOS9 system with a separate selfie, a screen photo album, Just using assetslibary now is not a good way to support photo gallery. In IOS8, Apple offers a new framework photokit that is better than older assetslibary,photokit and works seamlessly with the device and photo library. But in today's Apple users, the iOS7 system still occupies a large proportion, so assetslibary still have the use of meaning.

Assetslibary Deleting photos (for iOS7)

1. Add a photo to the album

    ALAssetsLibrary *lib = [[ALAssetsLibrary alloc]init];    UIImage *image = [UIImage imageNamed:@"pet"];    [lib writeImageToSavedPhotosAlbum:image.CGImage metadata:@{} completionBlock:^(NSURL *assetURL, NSError *error) { NSLog(@"Write image(%@) to album . Error:(%@)", assetURL, error); }];

2. Delete photos in albums

Alassetslibrary *lib = [[Alassetslibrary alloc]init]; [Lib Enumerategroupswithtypes:alassetsgroupsavedphotos usingblock:^ (Alassetsgroup *group,BOOL *stop) {[Group enumerateassetsusingblock:^ (Alasset *result, nsuinteger index, BOOL *stop) { if (re Sult. iseditable) { //here ImageData and metadata are set to nil, you can delete the photos in the album [Result setimagedata:nil metadata:Nil completionblock:^ (nsurl *asseturl, nserror *error) { NSLog (@ "Asset URL (%@) should be delete.) error:%@ ", Asseturl, error);}];}}];} failureblock:^ (nserror *error) {}];            
Photokit Deleting photos (for iOS8, iOS9)

1. Add a photo to the album

    First get the collection of albums Phfetchresult *collectonresuts = [phassetcollection fetchassetcollectionswithtype: Phassetcollectiontypesmartalbum Subtype:phassetcollectionsubtypeany options:[phfetchoptions New];To get to the collection for traversal [Collectonresuts enumerateobjectsusingblock:^ (ID obj,Nsuinteger idx,BOOL *stop) {phassetcollection *assetcollection = obj;Camera Roll is the album where we write photosif ([assetcollection. Localizedtitle isequaltostring:@ "Camera Roll"]) {[[Phphotolibrary sharedphotolibrary] performchanges:^{//request to create a asset Phassetchangerequest *assetrequest = [phassetchangerequest creationrequestforassetfromimage:[ UIImage imagenamed:@ "Pet"]; //request Edit album Phassetcollectionchangerequest *collectonrequest = [ Phassetcollectionchangerequest Changerequestforassetcollection:assetcollection]; //create a placeholder for asset, put it in the album edit request Phobjectplaceholder *placeholder = [assetrequest Placeholderforcreatedasset]; //album add photos [Collectonrequest Addassets:@[placeholder];} completionhandler:^ (bool success, nserror *error) {nslog (@ "error:%@", Error);}]; } }];

2. Delete photos in albums

Phfetchresult *collectonresuts = [Phassetcollection fetchassetcollectionswithtype:phassetcollectiontypesmartalbum    Subtype:phassetcollectionsubtypeany options:[phfetchoptions New]; [Collectonresuts enumerateobjectsusingblock:^ (ID obj,Nsuinteger idx,BOOL *stop) {phassetcollection *assetcollection = obj;if ([assetcollection. Localizedtitle isequaltostring:@ "Camera Roll"]) {Phfetchresult *assetresult = [ Phasset fetchassetsinassetcollection:assetcollection options:[phfetchoptions New]; [Assetresult enumerateobjectsusingblock:^ (ID obj, nsuinteger idx, BOOL *stop) {[phphotolibrary Sharedphotolibrary] performchanges:^{ //Get the last photo of the album if (idx = = [Assetresult count]- 1) {[Phassetchangereque St Deleteassets:@[obj]]; }} completionhandler:^ (BOOL success, Nserror *error) { NSLog (@ "error:%@", error);}]; } }];

IOS Delete photos from albums-from Jane's book

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.