swift - 如何使用collectionview将高质量图像发送到另一个 View

标签 swift

我的问题很简单。我有一个 collectionView,我将图像从它发送到另一个 View 并显示它。但问题是质量实在是太差了。我使用 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 public var 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 的照片。如果我更改为更高,那么我的应用程序会因为内存而自动崩溃。也许当用户单击单元格时可以获得完整质量的图像?

最佳答案

只要您有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)
}

关于swift - 如何使用collectionview将高质量图像发送到另一个 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52673697/

相关文章:

swift - 协议(protocol)是否可以默认实现静态工厂方法?

swift - parse.com 排序嵌套查询

ios - 滚动时隐藏导航栏而不隐藏状态栏及其背景

ios - POST 数据未传递到其他 View Controller

swift - 屏幕上绘制像素速度非常快

ios - Swift - 如何自动大写 UIButton 的标签?

ios - 如何让WKWebView在后台显示

android - 创建从 iOS (Swift) 到 Android(Kotlin) 的 Needle GaugeView

ios - 如何获取 SWIFT 3 中特定控件的标识符名称(通过 Storyboard创建)以从数据库设置值

ios - Xcode 6 Sprite 套件 - SourceKitService 终止