简体   繁体   English

Swift PHImageManager图片库图像选择了收集视图单元格后获取PHImageManagerMaximumSize

[英]Swift PHImageManager Photo Gallery Image Get PHImageManagerMaximumSize after collection view cell selected

My scenario, I am loading photo gallery photos into horizontal collection view cell Imageview. 我的情况是,我正在将图库照片加载到水平集合视图单元Imageview中。 For quick image loading purpose I gave PHImagemanager targetSize: CGSize(width: 120.0, height: 120.0) . 为了快速加载图像,我给了PHImagemanager targetSize: CGSize(width: 120.0, height: 120.0) Now once user sleeted cell in horizontal CollectionView need to get PHImageManagerMaximumSize actual high quality photo for preview Image. 现在,一旦用户在水平CollectionView中倾斜了单元格,就需要获取PHImageManagerMaximumSize实际高质量的照片作为预览图像。 How to achieve it? 如何实现呢?

cell.tag = Int(manager.requestImage(for: asset,
                                            targetSize: CGSize(width: 120.0, height: 120.0),  //PHImageManagerMaximumSize
                                            contentMode: .aspectFill,
                                            options: option) { (result, _) in
                                            cell.picImage?.image = result

}) })

// MARK: - UICollectionViewDelegate protocol
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {

        let selectedCell = collectionView.cellForItem(at: indexPath) as! GalleryCollectionViewCell
        previewImage.image = selectedCell.picImage.image // here I need to get full size exact image (not cropped image)
}

You should save 2 image. 您应该保存2张图片。 First is full original image, second is cropped image. 首先是完整的原始图像,其次是裁剪的图像。

Firstly: When user select image, save original image to disk ( crop image with protect aspect ratio and careful for image size ) and the same image crop again ( 120x120 ) and save disk too. 首先:当用户选择图像时,将原始图像保存到磁盘(裁剪图像时要保护长宽比,并注意图像尺寸),然后再次裁剪相同的图像(120x120)并保存磁盘。

And read from disk to memory when cell visible. 并在单元可见时从磁盘读取到内存。

Be careful! 小心! If images too huge, your memory usage increase too much and OS will be crash your app so don't keep too much images in your memory 如果图像太大,则内存使用量会增加太多,操作系统将使您的应用程序崩溃,因此请不要在内存中保留太多图像

You can set buffer images for visible cell count. 您可以为可见的细胞计数设置缓冲图像。

And that if user select cell, you can show original image 如果用户选择单元格,则可以显示原始图像

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM