【问题标题】:get thumbnail images in list and full size image when click on the item of list单击列表项时获取列表中的缩略图图像和全尺寸图像
【发布时间】:2017-02-15 09:40:03
【问题描述】:

所以我使用下面的代码从运行良好的库中获取所有图像:

func grabPhotos(){

   let imgManager = PHImageManager.default()
   let requestOptions = PHImageRequestOptions()
   requestOptions.isSynchronous = true
   requestOptions.deliveryMode = .highQualityFormat
   let fetchOptions = PHFetchOptions()
   fetchOptions.sortDescriptors = [NSSortDescriptor(key: "creationDate", ascending: true)]
   if let fetchResults : PHFetchResult = PHAsset.fetchAssets(with: .image, options: fetchOptions){

      if fetchResults.count>0{

       for i in 0..<fetchResults.count{

         imgManager.requestImage(for: fetchResults.object(at: i), targetSize: CGSize(width:100, height: 100), contentMode: .aspectFill, options: requestOptions, resultHandler: {

         image, error in
        self.Galleryimages.append(image!)

        print("array count is ",self.Galleryimages.count)
        self.photoCollectionview.reloadData()
      })
      }

    }
  }
}

我在 UICollectionView 中显示了所有图像,但在单击任何缩略图时我都找不到任何方法来获取原始图像。当用户单击 UICollectionView 中填充的任何缩略图时,我想获取原始图像(全尺寸图像)。

谢谢。

【问题讨论】:

  • 表示你想做什么?
  • 请看我编辑的问题,当用户点击任何缩略图时,我实际上想获取原始大小的图像。
  • 您不是将所有图像都存储在一个数组中。无法获取原图是什么意思?
  • 实际上,您会从资产中获取所有图像并显示到 collectioview 中,然后这些都是原始图像。你为什么说我没有得到原始图像?
  • @ KrishnaCA:使用我提到的代码,我得到了 100*100 大小的图像,用作缩略图,但是当用户点击任何图像时,我需要获取原始大小的图像。这里的原始图像是指具有原始大小的图像。

标签: ios iphone swift3 image-gallery phfetchoptions


【解决方案1】:

加载缩略图。

做了太多事情后得到了解决方案,可能对其他人有帮助。以下是执行此操作的步骤。

第一步:声明PHFetchResult对象

var Galleryimages: PHFetchResult<PHAsset>!

第 2 步:使用以下代码从图库中获取结果:

func grabPhotos(){

Galleryimages = PHAsset.fetchAssets(with: PHAssetMediaType.image, options: nil)

}

第 3 步:使用以下代码在您的 UI(集合视图/表格视图)中显示缩略图:

let imageview = cell.viewWithTag(1) as! UIImageView

PHImageManager.default().requestImage(for: (Galleryimages?[indexPath.row])!, targetSize: CGSize(width: 200, height: 200), contentMode: .aspectFill, options: nil) { (image: UIImage?, info: [AnyHashable: Any]?) -> Void in
  imageview.image = image
}

第 4 步: 最后使用以下代码获得完整尺寸的图像。

let options = PHImageRequestOptions()
options.deliveryMode = .highQualityFormat
options.resizeMode = .exact

PHImageManager.default().requestImage(for: (Galleryimages[indexPath.row]), targetSize: PHImageManagerMaximumSize, contentMode: .aspectFill, options: options) { (image: UIImage?, info: [AnyHashable: Any]?) -> Void in
  if let image = image {
    //Use this originan image
  }
}

【讨论】:

    【解决方案2】:

    当您从 PHAsset 获取时,您正在调整图像大小。所以用

    targetsize : PHImageManagerMaximumSize
    

    从这个 U 可以得到原始图像的原始大小。对于您的收藏视图,您可以直接从中制作缩略图并显示图像。所以当用户点击缩略图时,现在你可以显示原始图像了

    请使用 autoreleasepool 进行内存管理。

    for(PHAsset *asset in self.assets) {
        // This autorelease pool seems good (a1)
        autoreleasepool {
            NSLog(@"started requesting image %i", i);
            [[PHImageManager defaultManager] requestImageForAsset:asset targetSize:PHImageManagerMaximumSize contentMode:PHImageContentModeAspectFit options:[self imageRequestOptions] resultHandler:^(UIImage *image, NSDictionary *info) {
                dispatch_async(dispatch_get_main_queue(), ^{
                    //you can add autorelease pool here as well (a2)
                    @autoreleasepool {
                        assetCount++;
                        NSError *error = [info objectForKey:PHImageErrorKey];
                        if (error) NSLog(@"Image request error: %@",error);
                        else {
                            NSString *imagePath = [appDelegate.docsPath stringByAppendingPathComponent:[NSString stringWithFormat:@"%i.png",i]];
                            NSData *imageData = UIImagePNGRepresentation(image);
                            if(imageData) {
                                [imageData writeToFile:imagePath atomically:YES];
                                [self.imagesArray addObject:imagePath];
                            }
                            else {
                                NSLog(@"Couldn't write image data to file.");
                            }
                            [self checkAddComplete];
                            NSLog(@"finished requesting image %i", i);
                        }
                    } //a2 ends here
                });
            }];
            i++;
        } // a1 ends here
    }
    

    【讨论】:

    • 使用PHImageManagerMaximumSize 作为参数是正确的。但是,仅为缩略图加载全尺寸图像是不值得的。您最好仅在单击图像缩略图并需要显示全尺寸图像时才请求全尺寸图像。
    • @JeckyModi 如果我使用 PHImageManagerMaximumSize 作为目标大小,我的应用程序会因为内存压力而崩溃。那就是我使用 200 x 200 作为目标大小来避免崩溃。你能在你的答案中解释完整的方式吗?
    • @SumeetPurohit 您可以按照答案stackoverflow.com/questions/33274791/… 使用自动释放池,请随意查看答案
    • @JeckyModi 我们现在在 ARC 中,所以 autoreleasepool 现在无法工作。
    • 我在 swift autoreleasepool(invoking: Result#>) 中得到了这一行
    猜你喜欢
    • 2015-12-14
    • 1970-01-01
    • 2010-10-02
    • 2018-07-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-09
    相关资源
    最近更新 更多