如何使用PHCachingImageManager()。stopCachingImages

时间:2017-07-03 10:10:45

标签: ios swift phasset

我希望在集合视图中从相机胶卷中获取图像,并且在选择时我必须以全屏显示所选图像。所以为了达到这个目的,我使用了以下代码:

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell
{
    let cell : ImagesCell = collectionView.dequeueReusableCell(withReuseIdentifier: "ImagesCell", for: indexPath) as! ImagesCell
    let asset = assets!.object(at: indexPath.row)
    PHImageManager.default().requestImage(for: asset, targetSize: CGSize(width: 150, height: 150), contentMode: .aspectFill, options: nil)
    {
        (image, info) -> Void in
        cell.imageThumb.image = image
    }
    return cell
}

我的屏幕如下所示:

Photo1

现在点击didSelectRow委托中的单元格,我必须以全屏显示图像

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath)
{
    let asset = assets!.object(at: indexPath.row)
    guard (asset.mediaType == PHAssetMediaType.image) else {
        print("Not a valid image media type")
        return
    }

    //PHCachingImageManager().stopCachingImages(for: [asset], targetSize: CGSize(width: 150, height: 150), contentMode: .aspectFill, options: nil)

    PHImageManager.default().requestImage(for: asset, targetSize: CGSize(width:SCREENWIDTH(),height:SCREENHEIGHT()), contentMode: .aspectFit, options: nil)
    {
        (image, info) -> Void in
        runOnMainThreadWithoutDeadlock
        {
            let storybd = UIStoryboard(name: "MediaPicker", bundle: nil)
            let previewVC = storybd.instantiateViewController(withIdentifier: "PreviewViewController") as! PreviewViewController
            previewVC.selectedImage = image
            self.present(previewVC, animated: true, completion: nil)
        }
    }
}

屏幕如下所示:

Photo2

正如您所看到的,图像看起来模糊,即它需要缓存的图像,我想显示完整尺寸的图像,但我找不到相同的方法。

请分享是否有人有想法,试图找到一种方法。

感谢。

1 个答案:

答案 0 :(得分:2)

经过大量的反复试验:

PHImageManager.default().requestImageData(for: asset, options: nil) { (data, str, orientation, info) in
        let formAnImage = UIImage(data: data!)
        //you will get an original image
    }

上述选项并非适用于所有情况,如果用户选择较旧的图像,则数据为零,所以我使用了以下解决方案:

            let options = PHImageRequestOptions()
            options.isNetworkAccessAllowed = true
            options.isSynchronous = true
            options.resizeMode = PHImageRequestOptionsResizeMode.exact
            let targetSize = CGSize(width:SCREENWIDTH(), height:SCREENHEIGHT())

            PHImageManager.default().requestImage(for: asset, targetSize: targetSize, contentMode: PHImageContentMode.aspectFit, options: options) { (receivedImage, info) in

                if let formAnImage = receivedImage
                {
                    //You will get image here..
                }
            }