如何使用CollectionView将高质量图像发送到另一个视图

时间:2018-10-05 21:52:24

标签: swift

我的问题很简单。我有一个collectionView,并将图像从它发送到另一个视图并显示它。但是问题是质量真的很差。我使用youtube上的教程从照相馆制作了collectionView。这是我发送图像的代码。

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {

    let selectedCell = collectionView.cellForItem(at: indexPath) as! CollectionViewCell
    ujaj = selectedCell.imageView.image
    performSegue(withIdentifier: "segue077", sender: self)

}

还有我的完整代码:

导入UIKit 导入照片 ViewController类:UIViewController,UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout {     @IBOutlet公共变量bandau:UIImageView!

var imageArray = [UIImage]()

override func viewDidLoad() {
    grabPhotos()
}

func grabPhotos(){

    let imgManager = PHImageManager.default()

    let requestOptions = PHImageRequestOptions()
    requestOptions.isSynchronous = true
    requestOptions.deliveryMode = .highQualityFormat // Quality of images

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

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

        if fetchResult.count > 0 {
            for i in 0..<fetchResult.count {
                imgManager.requestImage(for: fetchResult.object(at: i) as! PHAsset, targetSize: CGSize(width: 200, height: 200), contentMode: .aspectFill, options: requestOptions, resultHandler: {
                    image, error in
                    self.imageArray.append(image!)

                })
            }
        }
        else {
            print("You got no photos")
           //self.collectionView?.reloadData()
        }
    }

}

@IBOutlet weak var fakk: UIImageView!

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

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


    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Cell", for: indexPath) as! CollectionViewCell

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




    imageView.image = imageArray[indexPath.row]



    return cell
}


public var ujaj: UIImage?


@IBAction func kadsasd(_ sender: Any) {
    performSegue(withIdentifier: "segue077", sender: self)
}

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {

    let selectedCell = collectionView.cellForItem(at: indexPath) as! CollectionViewCell
    ujaj = selectedCell.imageView.image
    performSegue(withIdentifier: "segue077", sender: self)

}

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
    let width = collectionView.frame.width / 3 - 1

    return CGSize(width: width, height: width)
}

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
    return 1.0
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
    return 1.0
}



override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    let main = segue.destination as! display



    main.image = ujaj
}

问题是如何发送完整质量的图像?因为现在我有200x200张照片。如果我更改为更高版本,则我的应用程序会由于内存而自动崩溃。当用户单击单元格时,也许可以获得完整质量的图像吗?

1 个答案:

答案 0 :(得分:0)

只要您拥有indexPath,建议您将其转换为Integer,然后再次要求从图库中获取照片。不要忘记将.targetSize更改为PHImageManagerMaximumSize,也将.contentMode更改为PHImageContentMode.default

您的情况:

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {




    let index : Int = indexPath.row


    let imgManager = PHImageManager.default()

    let requestOptions = PHImageRequestOptions()
    requestOptions.isSynchronous = true
    requestOptions.deliveryMode = .highQualityFormat // Quality of images

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


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

        imgManager.requestImage(for: fetchResult.object(at: index) , targetSize: PHImageManagerMaximumSize, contentMode: PHImageContentMode.default, options: requestOptions, resultHandler: {
            image, error in
            self.ujaj = image

        })

    performSegue(withIdentifier: "segue077", sender: self)
}