IOS获取系统相簿里的照片
代码转自:http://blog.csdn.net/shijiucdy/article/details/7404079
#import <AssetsLibrary/AssetsLibrary.h>
-(void)getImgs{
dispatch_async(dispatch_get_main_queue(), ^{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
ALAssetsLibraryAccessFailureBlock failureblock = ^(NSError *myerror){
NSLog(@"相册访问失败 =%@", [myerror localizedDescription]);
if ([myerror.localizedDescription rangeOfString:@"Global denied access"].location!=NSNotFound) {
NSLog(@"无法访问相册.请在'设置->定位服务'设置为打开状态.");
}else{
NSLog(@"相册访问失败.");
}
};
ALAssetsGroupEnumerationResultsBlock groupEnumerAtion = ^(ALAsset *result, NSUInteger index, BOOL *stop){
if (result!=NULL) {
if ([[result valueForProperty:ALAssetPropertyType] isEqualToString:ALAssetTypePhoto]) {
[self._dataArray addObject:result];
}
}
};
ALAssetsLibraryGroupsEnumerationResultsBlock
libraryGroupsEnumeration = ^(ALAssetsGroup* group, BOOL* stop){
if (group == nil)
{
[self myMethod:self._dataArray];
}
if (group!=nil) {
NSString *g=[NSString stringWithFormat:@"%@",group];//获取相簿的组
NSLog(@"gg:%@",g);//gg:ALAssetsGroup - Name:Camera Roll, Type:Saved Photos, Assets count:71
/*这段代码 实际上也是有点多余的。
NSString *g1=[g substringFromIndex:16 ] ;
NSArray *arr=[[NSArray alloc] init];
arr=[g1 componentsSeparatedByString:@","];
NSString *g2=[[arr objectAtIndex:0] substringFromIndex:5];
if ([g2 isEqualToString:@"Camera Roll"]) {
g2=@"相机胶卷";
}
NSString *groupName=g2;//组的name
*/
[group enumerateAssetsUsingBlock:groupEnumerAtion];
}
//该段代码为块代码执行结束之后运行的部分。对于有刷新UI或者写入数据库的人来说也许是有必要的。
/*else{
dispatch_async(dispatch_get_global_queue(0,0),^{
[self myMethod:self._dataArray];
});
}*/
};
ALAssetsLibrary* library = [[ALAssetsLibrary alloc] init];
[library enumerateGroupsWithTypes:ALAssetsGroupAll
usingBlock:libraryGroupsEnumeration
failureBlock:failureblock];
// [library release];
[pool release];
});
}
//------------------------根据图片的url反取图片-----
ALAssetsLibrary *assetLibrary=[[ALAssetsLibrary alloc] init];
NSURL *url=[NSURL URLWithString:urlStr];
[assetLibrary assetForURL:url resultBlock:^(ALAsset *asset) {
UIImage *image=[UIImage imageWithCGImage:asset.thumbnail];
cellImageView.image=image;
}failureBlock:^(NSError *error) {
NSLog(@"error=%@",error);
}
];
//---------------------