【iOS开发】---- 如何将图片保存至自定义分组


      一般我们照片存储都是直接存储在相机胶卷中,调用下面的方法就可以了:




    1. void UIImageWriteToSavedPhotosAlbum (  
    2.    UIImage  *image,  
    3.    id       completionTarget,  
    4.    SEL      completionSelector,  
    5.    void     *contextInfo  
    6. );



           但是如果我们想将图片存储到自定义的分组该怎么做呢,我查了一下ALAssetsLibrary的文档,发现没有相应的方法,好吧,google一下,发现了一篇文章:点击打开链接


    作者的解决思路如下:

    1.将图片先保存到相机胶卷中;

    2.在相机胶卷中找到这个图片所创建的AlAsset;

    3.将此AlAsset添加到你需要存放的自定义分组中。


          主要代码如下所示,注释也很清楚了:


      1. -(void)saveImage:(UIImage*)image toAlbum:(NSString*)albumName withCompletionBlock:(SaveImageCompletion)completionBlock  
      2. {  
      3.     //write the image data to the assets library (camera roll)  
      4.     [self writeImageToSavedPhotosAlbum:image.CGImage orientation:(ALAssetOrientation)image.imageOrientation   
      5.                         completionBlock:^(NSURL* assetURL, NSError* error) {  
      6.                                 
      7.                           //error handling  
      8.                           if (error!=nil) {  
      9.                               completionBlock(error);  
      10.                               return;  
      11.                           }  
      12.   
      13.                           //add the asset to the custom photo album  
      14.                           [self addAssetURL: assetURL   
      15.                                     toAlbum:albumName   
      16.                         withCompletionBlock:completionBlock];  
      17.                             
      18.                       }];  
      19. }



        1. -(void)addAssetURL:(NSURL*)assetURL toAlbum:(NSString*)albumName withCompletionBlock:(SaveImageCompletion)completionBlock  
        2. {  
        3.     __block BOOL albumWasFound = NO;  
        4.       
        5.     //search all photo albums in the library  
        6.     [self enumerateGroupsWithTypes:ALAssetsGroupAlbum   
        7.                         usingBlock:^(ALAssetsGroup *group, BOOL *stop) {  
        8.   
        9.                             //compare the names of the albums  
        10.                             if ([albumName compare: [group valueForProperty:ALAssetsGroupPropertyName]]==NSOrderedSame) {  
        11.                                   
        12.                                 //target album is found  
        13.                                 albumWasFound = YES;  
        14.                                   
        15.                                 //get a hold of the photo's asset instance  
        16.                                 [self assetForURL: assetURL   
        17.                                       resultBlock:^(ALAsset *asset) {  
        18.                                                     
        19.                                           //add photo to the target album  
        20.                                           [group addAsset: asset];  
        21.                                             
        22.                                           //run the completion block  
        23.                                           completionBlock(nil);  
        24.                                             
        25.                                       } failureBlock: completionBlock];  
        26.   
        27.                                 //album was found, bail out of the method  
        28.                                 return;  
        29.                             }  
        30.                               
        31.                             if (group==nil && albumWasFound==NO) {  
        32.                                 //photo albums are over, target album does not exist, thus create it  
        33.                                   
        34.                                 __weak ALAssetsLibrary* weakSelf = self;  
        35.   
        36.                                 //create new assets album  
        37.                                 [self addAssetsGroupAlbumWithName:albumName   
        38.                                                       resultBlock:^(ALAssetsGroup *group) {  
        39.                                                                     
        40.                                                           //get the photo's instance  
        41.                                                           [weakSelf assetForURL: assetURL   
        42.                                                                         resultBlock:^(ALAsset *asset) {  
        43.   
        44.                                                                             //add photo to the newly created album  
        45.                                                                             [group addAsset: asset];  
        46.                                                                               
        47.                                                                             //call the completion block  
        48.                                                                             completionBlock(nil);  
        49.   
        50.                                                                         } failureBlock: completionBlock];  
        51.                                                             
        52.                                                       } failureBlock: completionBlock];  
        53.   
        54.                                 //should be the last iteration anyway, but just in case  
        55.                                 return;  
        56.                             }  
        57.                               
        58.                         } failureBlock: completionBlock];  
        59. }



        作者写了一个小demo:CustomAlbumDemo.zip 上面的代码用到了ARC,如果你的工程没有使用ARC,记得做相应处理。

        如何添加分组:

          1. [[DataCenter defaultAssetsLibrary] addAssetsGroupAlbumWithName:[alertView textFieldAtIndex:0].text  
          2.                                                    resultBlock:resulBlock  
          3.                                                   failureBlock:nil];  
          4.