2016年4月8日 星期五

讀取相機菲林最後的相片

在參考其他的拍照應用程式時,發現有程式能顯示相機菲林的最新相片。已很久沒有做過相關的東西,不知道原來在 iOS 8 開始,相簿方面的支援已經改進不少。查找過資料,要讀取相機菲林最後的相片是這樣做:
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(160, 20, 100, 100)];
[self.view addSubview:imageView];

PHFetchResult *assetCollection = [PHAssetCollection fetchAssetCollectionsWithType:PHAssetCollectionTypeSmartAlbum subtype:PHAssetCollectionSubtypeSmartAlbumUserLibrary options:nil];
[assetCollection enumerateObjectsUsingBlock:^(PHAssetCollection *smartFolderAssetCollection, NSUInteger index, BOOL *stop)  {
   PHLog(@"smartFolderAssetCollection: %@", smartFolderAssetCollection);

   PHFetchResult *result = [PHAsset fetchAssetsInAssetCollection:smartFolderAssetCollection options:nil];
   [result enumerateObjectsUsingBlock:^(PHAsset *asset, NSUInteger index, BOOL *stop)  {
      PHLog(@"asset: %@", asset);
   }];

   PHAsset *asset = [result lastObject];
   [[PHImageManager defaultManager] requestImageForAsset:asset targetSize:CGSizeMake(100, 100) contentMode:PHImageContentModeAspectFit options:nil resultHandler:^(UIImage *result, NSDictionary *info)  {
      [imageView setImage:result];
   }];
}];

沒有留言: