2017-07-03 28 views
0

私は、コレクションビューでカメラロールから画像をフェッチしたいのですが、選択した画像をフルスクリーンで表示する必要があります。PHCachingImageManager()の使い方stopCachingImages

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

ご覧のとおり、画像がぼやけて見えます。つまり、キャッシュされた画像が表示されます。フルサイズの画像を表示したいのですが、同じ方法を見つけることができません。

誰かがアイデアを持っていれば分かち合い、長い間道を探してください。

ありがとうございました。試行錯誤の多くの後

答えて

2

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

上記のオプションは、ユーザーが古い画像を選択した場合、データがnilであるので、私は解決策の下に使用している、すべてのケースのために完璧に働いていなかった。

  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.. 
       } 
      }