[原]iOS7.1获取图库所有照片的方法封装
图片转换取出需要转换方向!!
- (int)imageOrientation { return [[self valueForProperty:ALAssetPropertyOrientation] intValue]; } //高清图 - (UIImage *)fastGetFullResolutionImage { CGImageRef ref = [[self defaultRepresentation] fullResolutionImage]; return [UIImage imageWithCGImage:ref scale:1 orientation:[self imageOrientation]]; }
简单类快捷入口:链接: http://pan.baidu.com/s/1bnxvsiN 密码: 73ud
//出处:http://www.cnblogs.com/madordie/ // // 声明:写此类时候只为了获取图库中的图片。由于真机不在手边,只在模拟器上测试(iOS7.1) // 如需要获得其他视频之类的可以自行更改fillAssetGroups函数 // // // 本地照片管理 // (单例模式) // 用法: //JGPhotoAlbumManager *manager = [JGPhotoAlbumManager shared]; //[manager setSaveAllPhotos:^(NSArray *images) { // if (!_tableData) { // _tableData = [[NSMutableArray alloc] init]; // } // [_tableData removeAllObjects]; // [_tableData addObjectsFromArray:images]; // [_tableView reloadData]; //}]; // // cell 填充: //ALAsset *asset = _tableData[indexPath.row]; //[cell.imageView setImage:[asset fastGetThumbnailImage]]; //注: //fastGetThumbnailImage为自定义类别,见#import "ALAsset+JGFastGetPhoto.h" //
关键代码:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
//如果需要获取别的更改下面的addObject位置 #pragma mark - 填充 _assetGroups - (void)fillAssetGroups { ALAssetsLibrary *assetsLibrary = [JGPhotoAlbumManager sharedAssetsLibrary];//生成整个photolibrary句柄的实例 [assetsLibrary enumerateGroupsWithTypes:ALAssetsGroupAll usingBlock:^(ALAssetsGroup *group, BOOL *stop) {//获取所有group if (group) { [group enumerateAssetsUsingBlock:^(ALAsset *result, NSUInteger index, BOOL *stop) {//从group里面 NSString* assetType = [result valueForProperty:ALAssetPropertyType]; if ([assetType isEqualToString:ALAssetTypePhoto]) { NSLog(@"Photo"); [_assetGroups addObject:result]; }else if([assetType isEqualToString:ALAssetTypeVideo]){ NSLog(@"Video"); }else if([assetType isEqualToString:ALAssetTypeUnknown]){ NSLog(@"Unknow AssetType"); } NSDictionary *assetUrls = [result valueForProperty:ALAssetPropertyURLs]; NSUInteger assetCounter = 0; for (NSString *assetURLKey in assetUrls) { NSLog(@"Asset URL %lu = %@",(unsigned long)assetCounter,[assetUrls objectForKey:assetURLKey]); } NSLog(@"Representation Size = %lld",[[result defaultRepresentation]size]); }]; } else { //stop dispatch_async(dispatch_get_main_queue(), ^(void) { _saveAllPhotos(_assetGroups); }); } } failureBlock:^(NSError *error) { NSLog(@"Enumerate the asset groups failed."); }]; }
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
@interface ALAsset (JGFastGetPhoto) //图片URL - (NSString *)fastGetURLString; - (NSURL *)fastGetURL; //缩略图 - (UIImage *)fastGetThumbnailImage; //全屏图 - (UIImage *)fastGetFullScreenImage; //高清图 - (UIImage *)fastGetFullResolutionImage; //获取时间 - (NSString *)fastGetDataString; //缩略图 - (UIImage *)fastGetThumbnailImage { CGImageRef ref = [self thumbnail]; return [UIImage imageWithCGImage:ref]; }