iOS 開發: 自訂相簿, 儲存多張圖片到自訂相簿中

來源:互聯網
上載者:User

標籤:

1、自訂相簿(相容 iOS7 iOS8)

  • - (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;

 

2、把多張照片存入自訂的相簿中

  • 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];}

     

3、需要引入的標頭檔

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

     

ok~ 搞掂~ 有問題可以評論~ 一起探討~

iOS 開發: 自訂相簿, 儲存多張圖片到自訂相簿中

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.