显示系统相册中某个相簿的照片

首先导入AssetsLibrary.framework 并且加入ALAssetsLibrary+CustomPhotoAlbum.h第三方库(诸君可以去网上搜一下)

1.创建相簿,并且把图片保存到相簿中

 ALAssetsLibrary *assetsLibrary = [[[ALAssetsLibraryalloc] init]autorelease];    

    //image为需要保存的图片

    [assetsLibrarysaveImage:image toAlbum:@"杰合体"completionBlock:^(NSURL *assetURL, NSError *error) {

       NSLog(@"保存成功");

    }failureBlock:^(NSError *error) {

        NSLog(@"保存失败");

    }];



2.遍历所有相簿,找出我需要的相簿,之后遍历相应相簿中的所有图片


//这个我暂时没用,诸君可以用一下做一下保护

 ALAssetsLibraryAccessFailureBlock failureblock = ^(NSError *myerror){

            NSLog(@"相册访问失败 =%@", [myerrorlocalizedDescription]);

            if ([myerror.localizedDescription rangeOfString:@"Global denied access"].location!=NSNotFound) {

                  NSLog(@"无法访问相册.请在'设置->定位服务'设置为打开状态.");

            }else{

                NSLog(@"相册访问失败.");

            }

        };

ALAssetsLibrary *assetsLibrary = [[[ALAssetsLibrary alloc]init]autorelease];

   

   ALAssetsGroupEnumerationResultsBlock groupEnumerAtion = ^(ALAsset*result,NSUInteger index, BOOL *stop){

        if(result!=NULL) {

            

            if([[resultvalueForProperty:ALAssetPropertyType]isEqualToString:ALAssetTypePhoto]) {


               //获取图片的url

               NSString *urlstr=[NSStringstringWithFormat:@"%@",result.defaultRepresentation.url];

                NSLog(@"%@",urlstr);

                //获取缩略图

               CGImageRef  ref = [resultthumbnail];

               UIImage *img = [[UIImage alloc]initWithCGImage:ref];

                NSData*imgData = UIImageJPEGRepresentation(img, 1.0);

               

                //获取全屏图片

               CGImageRef fullScreenCGImage = [[result defaultRepresentation]fullScreenImage];

               UIImage *fullScreenImage = [[UIImagealloc]initWithCGImage:fullScreenCGImage];

                NSData*fullScreenImageData = UIImageJPEGRepresentation(fullScreenImage, 1.0);


               //为图片是否需要横屏显示加一个tag

                BOOLisSpanImage = (fullScreenImage.size.width >fullScreenImage.size.height)?YES:NO;

               NSLog(@"with:%f,height:%f",fullScreenImage.size.width,fullScreenImage.size.height);

               

                //获取创建图片文件的时间

                NSDate*dateTime = ( NSDate *)[result valueForProperty:ALAssetPropertyDate];

//将所需要的资料存入字典

               NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:

                                           urlstr,@"urlstr",

                                           imgData,@"dataObj",

                                           fullScreenImageData,@"fullScreenImageData",

                                           [KCCommon getDate:dateTime],@"imagedate",

                                            [KCCommongetWeek:dateTime],@"imageWeekDay",

                                           [NSNumber numberWithBool:isSpanImage],@"isSpanImage",nil];

               [self.array addObject:dic];

                if(index == imageCount-1)

//好了,到此位置,我从相册中取到的信息已全部进入array,以下方法为对图片信息的处理

                   [self setImageLocation];

            }

        }

       

    };

 

   

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

        if (!group) {

           

        }else{

//获取相簿的名字

            NSString*g=[NSString stringWithFormat:@"%@",group];

            NSString*g1=[g substringFromIndex:16 ] ;

            NSArray*arr=[NSArray arrayWithArray:[g1 componentsSeparatedByString:@","]];

            NSString *g2=[[arrobjectAtIndex:0]substringFromIndex:5];

           NSLog(@"%@",[[arr objectAtIndex:2]substringFromIndex:14]);

   //"杰合体"为相簿的名字

            if ([g2 isEqualToString:@"杰合体"]){

               imageCount =[[[arr objectAtIndex:2]substringFromIndex:14]intValue];

                [groupenumerateAssetsUsingBlock:groupEnumerAtion];

            }

        }

       

    }failureBlock:^(NSError *error) {

       NSLog(@"error:%@",error);

    }];

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值