iOS save photo in an app specific album

后端 未结 5 2107
天命终不由人
天命终不由人 2020-11-30 01:24

I\'m creating an iOS 5 app. I want to save a photo to the device.

I want to save the photo to an album specific to my app, so I need to create the album, and then sa

5条回答
  •  萌比男神i
    2020-11-30 01:59

    You may use the following code just change the name of album :

    __weak ALAssetsLibrary *lib = self.library;
    
    [self.library addAssetsGroupAlbumWithName:@"My Photo Album" resultBlock:^(ALAssetsGroup *group) {
    
        ///checks if group previously created
        if(group == nil){
    
            //enumerate albums
            [lib enumerateGroupsWithTypes:ALAssetsGroupAlbum
                               usingBlock:^(ALAssetsGroup *g, BOOL *stop)
             {
                 //if the album is equal to our album
                 if ([[g valueForProperty:ALAssetsGroupPropertyName] isEqualToString:@"My Photo Album"]) {
    
                     //save image
                     [lib writeImageDataToSavedPhotosAlbum:UIImagePNGRepresentation(image) metadata:nil
                                           completionBlock:^(NSURL *assetURL, NSError *error) {
    
                                               //then get the image asseturl
                                               [lib assetForURL:assetURL
                                                    resultBlock:^(ALAsset *asset) {
                                                        //put it into our album
                                                        [g addAsset:asset];
                                                    } failureBlock:^(NSError *error) {
    
                                                    }];
                                           }];
    
                 }
             }failureBlock:^(NSError *error){
    
             }];
    
        }else{
            // save image directly to library
            [lib writeImageDataToSavedPhotosAlbum:UIImagePNGRepresentation(image) metadata:nil
                                  completionBlock:^(NSURL *assetURL, NSError *error) {
    
                                      [lib assetForURL:assetURL
                                           resultBlock:^(ALAsset *asset) {
    
                                               [group addAsset:asset];
    
                                           } failureBlock:^(NSError *error) {
    
                                           }];
                                  }];
        }
    
    } failureBlock:^(NSError *error) {
    
    }];
    

提交回复
热议问题