如何使用 collectionview 将高质量图像发送到另一个视图
Posted
技术标签:
【中文标题】如何使用 collectionview 将高质量图像发送到另一个视图【英文标题】:How to send high quality images to another view using collectionview 【发布时间】:2018-10-05 21:52:24 【问题描述】:我的问题很简单。我有一个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 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 的照片。如果我更改为更高,那么我的应用程序会因为内存而自动崩溃。当用户点击单元格时,也许可以获得完整质量的图像?
【问题讨论】:
【参考方案1】:只要您有 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)
【讨论】:
以上是关于如何使用 collectionview 将高质量图像发送到另一个视图的主要内容,如果未能解决你的问题,请参考以下文章
如何使用 StoryBoard 在 UITableViewCell 内快速构建一个 collectionView
如何使用情节提要将 CollectionView 添加到 ViewController?