Swift - 使用Photos Framework选择时保持照片质量

时间:2017-03-17 08:41:59

标签: swift uicollectionview

我创建了一个UICollectionview,在其中我使用Photos Framework显示相册中的所有图片。点按照片可创建带有放大版本的新视图。

我的所有照片在我的collectionView中看起来都很漂亮,但是当点击时,该照片的分辨率会受到重创。我希望我的代码以原始格式和质量打开图像,就像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 个答案:

答案 0 :(得分:1)

问题是因为您已经以此尺寸(200x200)获取图像:

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!) })

您需要做的是在用户点击缩略图时(或新视图控制器加载时)请求图像并以最大尺寸请求图像。