2017-03-17 10 views
0

写真フレームワークを使用してフォトアルバムのすべての画像を表示するUICollectionviewを作成しました。写真をタップすると、ズームインした新しいビューが作成されます。Swift - 写真フレームワークを使用して選択すると写真の品質を保ちます

私のすべての写真は、コレクションビューできれいに見えますが、タップするとその写真の解像度が大幅に低下します。 iOSフォトアルバムのように、自分のコードが元の形式と品質でイメージを開くようにしたい。

func getPhotosFromAlbum() { 

    let imageManager = PHImageManager.default() 

    let requestOptions = PHImageRequestOptions() 
    requestOptions.isSynchronous = true 
    requestOptions.deliveryMode = .highQualityFormat 

    let fetchOptions = PHFetchOptions() 
    fetchOptions.sortDescriptors = [NSSortDescriptor(key: "creationDate", ascending: false)] 

    let fetchResult: PHFetchResult = PHAsset.fetchAssets(with: .image, options: fetchOptions) 

     if fetchResult.count > 0 { 

      for i in 0..<fetchResult.count { 

       imageManager.requestImage(for: fetchResult.object(at: i), targetSize: CGSize(width: 200, height: 200), contentMode: .aspectFill, options: requestOptions, resultHandler: { image, error in 

        self.imageArray.append(image!) 
       }) 
      } 

     } else { 
      self.collectionView?.reloadData() 
     } 
} 

// MARK: UICollectionViewDataSource 
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { 
    return imageArray.count 
} 

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { 

    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: cellId, for: indexPath) as! PhotosCollectionViewCell 

    cell.imageView.image = imageArray[indexPath.row] 
    cell.imageView.addGestureRecognizer(UIGestureRecognizer(target: self, action: #selector(tapGesture))) 

    return cell 
} 

var startingFrame: CGRect? 
var blackBackGroundView: UIView? 

func tapGesture(sender: UITapGestureRecognizer) { 
    if let indexPath = self.collectionView?.indexPathForItem(at: sender.location(in: self.collectionView)) { 

     let imageView = self.collectionView?.cellForItem(at: indexPath) 

     startingFrame = imageView?.superview?.convert((imageView?.frame)!, to: nil) 

     let zoomingImageView = UIImageView(frame: startingFrame!) 
     zoomingImageView.image = imageArray[indexPath.row] 
     zoomingImageView.isUserInteractionEnabled = true 
     zoomingImageView.contentMode = .scaleAspectFill 
     zoomingImageView.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(handleZoomOut))) 

     if let keyWindow = UIApplication.shared.keyWindow { 
      blackBackGroundView = UIView(frame: keyWindow.frame) 
      blackBackGroundView?.backgroundColor = UIColor.black 
      blackBackGroundView?.alpha = 0 

      keyWindow.addSubview(blackBackGroundView!) 
      keyWindow.addSubview(zoomingImageView) 

      UIView.animate(withDuration: 0.3, delay: 0, usingSpringWithDamping: 1, initialSpringVelocity: 1, options: .curveEaseOut, animations: { 

       self.blackBackGroundView?.alpha = 1 

       let height = self.startingFrame!.height/self.startingFrame!.width * keyWindow.frame.width 

       zoomingImageView.frame = CGRect(x: 0, y: 0, width: keyWindow.frame.width, height: height) 

       zoomingImageView.center = keyWindow.center 

       }, completion: {(completed) in 
        // Do nothing 
      }) 
     } 
    } 
} 

func handleZoomOut(tapGesture: UITapGestureRecognizer) { 
    if let zoomOutImageView = tapGesture.view { 

     UIView.animate(withDuration: 0.3, delay: 0, usingSpringWithDamping: 1, initialSpringVelocity: 1, options: .curveEaseOut, animations: { 
      zoomOutImageView.frame = self.startingFrame! 
      self.blackBackGroundView?.alpha = 0 

      }, completion: {(completed: Bool) in 
       zoomOutImageView.removeFromSuperview() 
     }) 
    } 
} 

答えて

1

あなたはこのサイズ(200×200)で画像をフェッチしてきましたので、問題は次のとおりです。

imageManager.requestImage(for: fetchResult.object(at: i), targetSize: CGSize(width: 200, height: 200), contentMode: .aspectFill, options: requestOptions, resultHandler: { image, error in self.imageArray.append(image!) })

あなたがする必要がどのような(ユーザーがサムネイルをタップしたときの画像を要求していますまたは新しいビューコントローラがロードされたとき)、最大サイズでリクエストします。

+0

あなたは正しいです!完全にサイズはフェッチ中に既に設定されていたという私の心を滑り込ませた。答えとしてマークされています。 –

関連する問題