2015-10-15 105 views
4

我是新的Photo框架,我不知道如何使用它。我使用了很多鏈接,但是我很困惑如何在imageview中顯示圖像。我如何使用目標c中的照片框架訪問照片庫中的照片

我想用照片框架來獲取從庫中的所有照片我試圖

NSMutableOrderedSet *recentsDataSource; 
PHFetchResult *assetCollection = [PHAssetCollection fetchAssetCollectionsWithType:PHAssetCollectionTypeSmartAlbum | PHAssetCollectionTypeAlbum subtype:PHAssetCollectionSubtypeAny options:nil]; 
    for (PHAssetCollection *sub in assetCollection) 
    { 
     PHFetchResult *assetsInCollection = [PHAsset fetchAssetsInAssetCollection:sub options:nil]; 

     for (PHAsset *asset in assetsInCollection) 
     { 
      [self.recentsDataSource addObject:asset]; 
     } 
    } 

    if (self.recentsDataSource.count > 0) 
    { 
     NSArray *array = [self.recentsDataSource sortedArrayUsingDescriptors:@[[NSSortDescriptor sortDescriptorWithKey:@"creationDate" ascending:NO]]]; 
     NSLog(@"Photo Array : %@",array); 
     self.recentsDataSource = [[NSMutableOrderedSet alloc]initWithArray:array]; 
    } 

我必須得到這個類型的數組。

照片陣列:( 「5A2ABD38-D724-42B1-939A-F557D5CCE9FC/L0/001 mediaType的= 1/0, sourceType的= 1,(328x328),creationDate = 2015年10月14日06:06 :22 + 0000, location = 0,hidden = 0,favorite = 0「, 」5391482F-9887-4518-9976-D5D544CDF8CA/L0/001 mediaType = 1/0, sourceType = 1,(450x800),creationDate = 2015-10-12 09:27:41 +0000, location = 0,hidden = 0,favorite = 1「, 」3B921984-6E79-4E4D-B70D-C7FFB0AC1C63/L0/001 mediaType = 1/0, sourceType = 1,(3000x2002),creationDate = 2012-08-08 09:25:30 +0000, location = 1,hidden = 0,favorite = 0「, 「D64D501E-F85D-4EE8-9C79-414272621A80/L0/001 mediaType = 1/0, sourceType = 1,(1668x2500),creationDate = 2012-08-08 08:59:49 +0000, location = 1, hidden = 0,favorite = 0「, 」46A57980-9C55-42A8-9792-6BD98B25F01D/L0/001 mediaType = 1/0, sourceType = 1,(3000x2002),creationDate = 2012-08-08 06:22: 1100000012,location = 1,hidden = 0,favorite = 0「, 」1E040EE9-99A9-4325-AB1F-CBF4E45111DA/L0/001 mediaType = 1/0, sourceType = 1,(4288x2848),creationDate = 2011-03-12 10:47:25 +0000, location = 1,hidden = 0,favorite = 1「, 」BF3A71CD-3065-43C0-A933-DD89BBE8C778/L0/001 mediaType = 1/0, sourceType = 1,(4288x2848),creationDate = 2009-10-09 08:39:20 +0000, location = 0,hidden = 0,favorite = 0「)

如何在imageview上顯示圖像。

回答

16

在.H/m文件添加此

#import <Photos/Photos.h> 

全局變量:

@property(nonatomic , strong) PHFetchResult *assetsFetchResults; 
@property(nonatomic , strong) PHCachingImageManager *imageManager; 

viewDidLoad中的代碼:

// Fetch all assets, sorted by date created. 
    PHFetchOptions *options = [[PHFetchOptions alloc] init]; 
    options.sortDescriptors = @[[NSSortDescriptor sortDescriptorWithKey:@"creationDate" ascending:NO]]; 
    _assetsFetchResults = [PHAsset fetchAssetsWithOptions:options]; 

    _imageManager = [[PHCachingImageManager alloc] init]; 

numberOfItemsInSection方法:

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section 
{ 
    return [_assetsFetchResults count]; 
} 

UICollectionView cellForItemAtIndexPath:代碼:

UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cellID" forIndexPath:indexPath]; 

UIImageView *imageView = (UIImageView *)[cell viewWithTag:101]; 
PHAsset *asset = _assetsFetchResults[indexPath.item]; 

     [_imageManager requestImageForAsset:asset targetSize:imageView.frame.size contentMode:PHImageContentModeAspectFill options:nil resultHandler:^(UIImage *result, NSDictionary *info) 
     { 
      imageView.image = result; 
     }]; 

[EDITED]

訪問圖像:

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath 
{ 
    PHAsset *asset = _assetsFetchResults[indexPath.item]; 

    [_imageManager requestImageForAsset:asset targetSize:CGSizeMake(200, 200) contentMode:PHImageContentModeAspectFill options:nil resultHandler:^(UIImage *result, NSDictionary *info) 
     { 
      // result is the actual image object. 
     }]; 
} 
+0

謝謝U,U保存箱。 – sohil

+0

我怎樣才能得到圖像日期明智 – sohil

+0

我編輯了那裏答案:) – Sneha