zoukankan      html  css  js  c++  java
  • 如何将图片保存至自定义分组

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

    void UIImageWriteToSavedPhotosAlbum (
       UIImage  *image,
       id       completionTarget,
       SEL      completionSelector,
       void     *contextInfo
    );


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

           作者的解决思路如下:

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

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

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


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

    -(void)saveImage:(UIImage*)image toAlbum:(NSString*)albumName withCompletionBlock:(SaveImageCompletion)completionBlock
    {
        //write the image data to the assets library (camera roll)
        [self writeImageToSavedPhotosAlbum:image.CGImage orientation:(ALAssetOrientation)image.imageOrientation 
                            completionBlock:^(NSURL* assetURL, NSError* error) {
                                  
                              //error handling
                              if (error!=nil) {
                                  completionBlock(error);
                                  return;
                              }
    
                              //add the asset to the custom photo album
                              [self addAssetURL: assetURL 
                                        toAlbum:albumName 
                            withCompletionBlock:completionBlock];
                              
                          }];
    }
    -(void)addAssetURL:(NSURL*)assetURL toAlbum:(NSString*)albumName withCompletionBlock:(SaveImageCompletion)completionBlock
    {
        __block BOOL albumWasFound = NO;
        
        //search all photo albums in the library
        [self enumerateGroupsWithTypes:ALAssetsGroupAlbum 
                            usingBlock:^(ALAssetsGroup *group, BOOL *stop) {
    
                                //compare the names of the albums
                                if ([albumName compare: [group valueForProperty:ALAssetsGroupPropertyName]]==NSOrderedSame) {
                                    
                                    //target album is found
                                    albumWasFound = YES;
                                    
                                    //get a hold of the photo's asset instance
                                    [self assetForURL: assetURL 
                                          resultBlock:^(ALAsset *asset) {
                                                      
                                              //add photo to the target album
                                              [group addAsset: asset];
                                              
                                              //run the completion block
                                              completionBlock(nil);
                                              
                                          } failureBlock: completionBlock];
    
                                    //album was found, bail out of the method
                                    return;
                                }
                                
                                if (group==nil && albumWasFound==NO) {
                                    //photo albums are over, target album does not exist, thus create it
                                    
                                    __weak ALAssetsLibrary* weakSelf = self;
    
                                    //create new assets album
                                    [self addAssetsGroupAlbumWithName:albumName 
                                                          resultBlock:^(ALAssetsGroup *group) {
                                                                      
                                                              //get the photo's instance
                                                              [weakSelf assetForURL: assetURL 
                                                                            resultBlock:^(ALAsset *asset) {
    
                                                                                //add photo to the newly created album
                                                                                [group addAsset: asset];
                                                                                
                                                                                //call the completion block
                                                                                completionBlock(nil);
    
                                                                            } failureBlock: completionBlock];
                                                              
                                                          } failureBlock: completionBlock];
    
                                    //should be the last iteration anyway, but just in case
                                    return;
                                }
                                
                            } failureBlock: completionBlock];
    }

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

          如何添加分组:

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


  • 相关阅读:
    Codeforces Round #481 (Div. 3) D. Almost Arithmetic Progression
    Codeforces Round #481 (Div. 3) G. Petya's Exams
    使用create-react-app 搭建react + ts + antd框架
    callback、promise和async、await的使用方法
    JS数组中Array.of()方法的使用
    react中替换关键字并且高亮显示的方法
    封装 jsonp请求数据的方法
    React的新特性 ---- Hooks ---- 的基本使用
    在canvas中使用其他HTML元素
    React的性能优化
  • 原文地址:https://www.cnblogs.com/aukle/p/3221904.html
Copyright © 2011-2022 走看看