iOS自定义相册

iOS自定义相册
转载请注明出处:http://blog.csdn.net/jensen2015/article/details/43408913

方法:
1
2
3
- ( void ) saveImage : ( UIImage * ) image
         toAlbum : ( NSString * ) albumName
withCompletionBlock : ( SaveImageCompletion ) completionBlock ;

1
#import <AssetsLibrary/AssetsLibrary.h>
1
@property ( strong , atomic ) ALAssetsLibrary * library ;
调用:
[ self . library saveImage :image toAlbum : @"Touch Code Magazine" withCompletionBlock : ^ ( NSError *error ) {
         if ( error != nil ) {
             NSLog ( @"Big error: %@" , [ error description ] ) ;
         }
     } ] ;
前提:(添加扩展方法)

#import <UIKit/UIKit.h>

#import <AssetsLibrary/AssetsLibrary.h>


typedefvoid(^SaveImageCompletion)(NSError* error);


@interface ALAssetsLibrary (CustomPhotoAlbum)


-(void)saveImage:(UIImage *)image toAlbum:(NSString*)albumName withCompletionBlock:(SaveImageCompletion)completionBlock;

-(void)addAssetURL:(NSURL*)assetURL toAlbum:(NSString*)albumName withCompletionBlock:(SaveImageCompletion)completionBlock;


@end



#import "ALAssetsLibrary+CustomPhotoAlbum.h"


@implementation ALAssetsLibrary (CustomPhotoAlbum)


-(void)saveImage:(UIImage*)image toAlbum:(NSString*)albumName withCompletionBlock:(SaveImageCompletion)completionBlock

{

    //write the image data to the assets library (camera roll)

    [selfwriteImageToSavedPhotosAlbum:image.CGImageorientation:(ALAssetOrientation)image.imageOrientation

                      completionBlock:^(NSURL* assetURL,NSError* error) {

                           

                          //error handling

                          if (error!=nil) {

                               completionBlock(error);

                              return;

                           }

                           

                           //add the asset to the custom photo album

                           [selfaddAssetURL: 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

    [selfenumerateGroupsWithTypes:ALAssetsGroupAlbum

                       usingBlock:^(ALAssetsGroup *group,BOOL *stop) {

                            

                            //compare the names of the albums

                           if ([albumName compare: [groupvalueForProperty:ALAssetsGroupPropertyName]]==NSOrderedSame) {

                                

                               //target album is found

                                albumWasFound =YES;

                                

                                //get a hold of the photo's asset instance

                                [selfassetForURL: assetURL

                                     resultBlock:^(ALAsset *asset) {

                                          

                                         //add photo to the target album

                                          [groupaddAsset: 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

                                [selfaddAssetsGroupAlbumWithName:albumName

                                                     resultBlock:^(ALAssetsGroup *group) {

                                                          

                                                         //get the photo's instance

                                                          [weakSelfassetForURL: assetURL

                                                                   resultBlock:^(ALAsset *asset) {

                                                                        

                                                                       //add photo to the newly created album

                                                                        [groupaddAsset: asset];

                                                                        

                                                                       //call the completion block

                                                                        completionBlock(nil);

                                                                        

                                                                    }failureBlock: completionBlock];

                                                          

                                                      }failureBlock: completionBlock];

                                

                                //should be the last iteration anyway, but just in case

                               return;

                            }

                            

                        }failureBlock: completionBlock];

    

}



@end


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值