iOS 開發: 自定義相冊, 保存多張圖片到自定義相冊中

一、自定義相冊(兼容 iOS7 iOS8)html

  • - (void)viewDidLoad {
    //search all photo albums in the library
        [assetsLibrary enumerateGroupsWithTypes:ALAssetsGroupAlbum usingBlock:^(ALAssetsGroup *group, BOOL *stop)
         {
             //compare the names of the albums
             if ([@"九噠" compare: [group valueForProperty:ALAssetsGroupPropertyName]]==NSOrderedSame) {
                 //target album is found
                 _isHaveAlbum = YES;
                 return;
             }
             if (group==nil && _isHaveAlbum == NO) {
                 //photo albums are over, target album does not exist, thus create it
                 //__weak ALAssetsLibrary* weakSelf = assetsLibrary;
                 
                 if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0) {
                     // iOS 8 code
                     // Create new album.
                     [[PHPhotoLibrary sharedPhotoLibrary] performChanges:^{
                         [PHAssetCollectionChangeRequest creationRequestForAssetCollectionWithTitle:@"九噠"];
                     } completionHandler:^(BOOL success, NSError *error) {
                         if (!success) {
                             NSLog(@"Error creating album: %@", error);
                         }
                     }];
                 }
                 else {
                     // iOS 7.x code
                     //create new assets album
                     [assetsLibrary addAssetsGroupAlbumWithName:@"九噠" resultBlock:nil failureBlock: nil];
                 }
                 //should be the last iteration anyway, but just in case
                 _isHaveAlbum = YES;
                 return;
             }
         } failureBlock: nil]; }

    具體的解釋我已經在上一篇博文(http://www.cnblogs.com/cai-rd/p/4432908.html)中解釋, 這裏就很少解釋了, 上面代碼須要補充一個變量 isHaveAblum = NO;spa

 

二、把多張照片存入自定義的相冊中code

  • for(int i = 0; i < 9; i++){
    // 利用延時每隔0.1秒存入 一次
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.1*i * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                            [self saveImage:yourImage toAlbum:@"Rd" withCompletionBlock:^{
                                NSLog(@"save s img success");
                            }];
    });
    
    }
    /**
     *  保存圖片到相冊中
     *
     *  @param image           <#image description#>
     *  @param albumName       <#albumName description#>
     *  @param completionBlock <#completionBlock description#>
     */
    -(void)saveImage:(UIImage*)image toAlbum:(NSString*)albumName withCompletionBlock:(void (^)(void))completionBlock
    {
        ALAssetsLibrary *assetsLibrary = [[ALAssetsLibrary alloc] init];
        //write the image data to the assets library (camera roll)
        [assetsLibrary 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:(void (^)(void))completionBlock
    {
        
        ALAssetsLibrary *assetsLibrary = [[ALAssetsLibrary alloc] init];
    
        __block BOOL albumWasFound = NO;
        
        //search all photo albums in the library
        [assetsLibrary 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
                [assetsLibrary assetForURL: assetURL resultBlock:^(ALAsset *asset)
                 {
                     //add photo to the target album
                     [group addAsset: asset];
                              
                     //run the completion block
                     completionBlock();
                 } failureBlock: nil];
                   
                //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 = assetsLibrary;
                
                if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0) {
                    // iOS 8 code
                    // Create new album.
                    [[PHPhotoLibrary sharedPhotoLibrary] performChanges:^{
                        [PHAssetCollectionChangeRequest creationRequestForAssetCollectionWithTitle:@"九噠"];
                    } completionHandler:^(BOOL success, NSError *error) {
                        if (!success) {
                            NSLog(@"Error creating album: %@", error);
                        }
                    }];
                }
                else {
                    // iOS 7.x code
                    //create new assets album
                    [assetsLibrary 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();
                          } failureBlock: nil];
                     } failureBlock: nil];
                    albumWasFound = YES;
                }
                
                //should be the last iteration anyway, but just in case
                return;
            }
        } failureBlock: nil];
    }

     

三、須要引入的頭文件orm

  • #import <AssetsLibrary/ALAsset.h>
    #import <AssetsLibrary/ALAssetsLibrary.h>
    #import <AssetsLibrary/ALAssetsGroup.h>
    #import <AssetsLibrary/ALAssetRepresentation.h>
    #import <Photos/Photos.h>

     

ok~ 搞掂~ 有問題能夠評論~ 一塊兒探討~htm

相關文章
相關標籤/搜索