【问题标题】:How can i access the photo from gallery using Photo framework in objective c我如何使用目标 c 中的照片框架从图库中访问照片
【发布时间】:2015-10-15 09:35:54
【问题描述】:

我是 Photo 框架的新手,我不知道如何使用它。我使用了很多链接,但我对如何在 imageview 中显示图像感到困惑。

我想使用我正在尝试的照片框架从图库中获取所有照片

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 媒体类型=1/0, sourceType=1, (328x328), creationDate=2015-10-14 06:06:22 +0000, 位置=0,隐藏=0,收藏=0“, " 5391482F-9887-4518-9976-D5D544CDF8CA/L0/001 mediaType=1/0, sourceType=1, (450x800), creationDate=2015-10-12 09:27:41 +0000, 位置=0,隐藏=0,收藏=1“, " 3B921984-6E79-4E4D-B70D-C7FFB0AC1C63/L0/001 媒体类型=1/0, sourceType=1, (3000x2002), creationDate=2012-08-08 09:25:30 +0000, 位置=1,隐藏=0,收藏=0“, " D64D501E-F85D-4EE8-9C79-414272621A80/L0/001 mediaType=1/0, sourceType=1, (1668x2500), creationDate=2012-08-08 08:59:49 +0000, 位置=1,隐藏=0,收藏=0“, " 46A57980-9C55-42A8-9792-6BD98B25F01D/L0/001 媒体类型=1/0, sourceType=1, (3000x2002), creationDate=2012-08-08 06:22:11 +0000, 位置=1,隐藏=0,收藏=0“, " 1E040EE9-99A9-4325-AB1F-CBF4E45111DA/L0/001 媒体类型=1/0, sourceType=1, (4288x2848), creationDate=2011-03-12 10:47:25 +0000, 位置=1,隐藏=0,收藏=1“, " BF3A71CD-3065-43C0-A933-DD89BBE8C778/L0/001 mediaType=1/0, sourceType=1, (4288x2848), creationDate=2009-10-09 08:39:20 +0000, 位置=0,隐藏=0,收藏=0")

如何在 imageview 上显示图像。

【问题讨论】:

    标签: ios objective-c photo-gallery


    【解决方案1】:

    将此添加到 .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;
             }];
    

    [编辑]

    访问图像:

    - (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.
             }];
    }
    

    【讨论】:

    • 它也会显示相同的结果,我想获取图像的日期。 :)
    • NSDate *creationDate; NSDate *修改日期;这是 PHAsset 的两个属性。您可以直接从资产对象中获取它。例如:asset.creationDate 或asset.modificationDate。
    • 您必须在 viewDidLoad 方法中进行更改并获取每个对象的 creationDate。制作一个以日期为键、对象为 NSArray 的字典,其中包含在该日期创建的所有 PHAsset。
    • 我找不到使用照片框架 API 的必要导入语句的任何其他示例。谢谢!愚蠢的苹果。 #import &lt;Photos/Photos.h&gt; 就是我所需要的
    • @sneha 删除特定图像的任何内容
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-09-01
    • 2015-10-20
    • 2014-08-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多