首先导入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);
}];