如何使用 NohanaImagePicker pod 文件查看图像?

Posted

技术标签:

【中文标题】如何使用 NohanaImagePicker pod 文件查看图像?【英文标题】:how can I see images with NohanaImagePicker pod file? 【发布时间】:2017-06-01 11:15:57 【问题描述】:

我正在使用 swift 3 并使用 NohanaImagePickerpod 文件来选择多个图像,它运行良好的问题是我想在集合视图中查看选定的图像,我将在这一行收到致命错误

        cell.imageforCV.image =  ViewController.imgArray[indexPath.row]

我对 imgArray 使用了静态变体

这是我的代码

import UIKit
import Photos
import NohanaImagePicker

class ViewController: UIViewController , NohanaImagePickerControllerDelegate , UICollectionViewDelegate , UICollectionViewDataSource 

static var imgArray = [UIImage]()
static var imgCount = Int()
@IBOutlet weak var imageCollectionView: UICollectionView!
var images = [ PHAsset]()
@IBAction func gallery(_ sender: UIButton) 

 let picker = NohanaImagePickerController()
    picker.delegate = self
    present(picker, animated: true, completion: nil)
    picker.maximumNumberOfSelection  = 200
    print("\(picker)")



 func nohanaImagePickerDidCancel(_ picker: NohanaImagePickerController) 
    print("Canceled")
    picker.dismiss(animated: true, completion: nil)


   func nohanaImagePicker(_ picker: NohanaImagePickerController, didFinishPickingPhotoKitAssets pickedAssts :[PHAsset]) 
    print("Completed\n\tpickedAssets = \(pickedAssts)")
    picker.dismiss(animated: true, completion: nil)
    performSegue(withIdentifier: "back", sender: self)
    let numberOfImages = pickedAssts.count
    print("\(numberOfImages)")


    ViewController.imgCount = pickedAssts.count

    var phAssetArray : [PHAsset] = []

    for i in 0..<pickedAssts.count
    
        ViewController.imgArray.append(self.getAssetThumbnail(asset: pickedAssts[i]));
    
    let size = CGSize(width: 50, height: 50)
    let options = PHImageRequestOptions()

    let manager : PHCachingImageManager =  PHCachingImageManager()


    manager.startCachingImages(for: phAssetArray,
                                                  targetSize: size,
                                                  contentMode: .aspectFill,
                                                  options: options)

    imageCollectionView.reloadData()
              


func getAssetThumbnail(asset: PHAsset) -> UIImage 
    var retimage = UIImage()
    print(retimage)
    let manager = PHImageManager.default()
    manager.requestImage(for: asset, targetSize: CGSize(width: 50.0, height: 50.0), contentMode: .aspectFit, options: nil, resultHandler: (result, info)->Void in
        retimage = result!
    )
    print(retimage)
    return retimage


func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int 
    return ViewController.imgArray.count



func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell 
    let cell = imageCollectionView.dequeueReusableCell(withReuseIdentifier: "imageCell", for: indexPath) as! imageCell

    print(ViewController.imgArray.count)
    print(indexPath.row)
    print(ViewController.imgArray[indexPath.row])



    cell.imageforCV.image =  ViewController.imgArray[indexPath.row]

    return cell




   override func viewDidLoad() 
         super.viewDidLoad()

         imageCollectionView.reloadData()

         self.imageCollectionView!.dataSource = self
         self.imageCollectionView!.delegate = self


    let picker = NohanaImagePickerController()

    // Set the maximum number of selectable images

    // Set the cell size
    picker.numberOfColumnsInPortrait = 2
    picker.numberOfColumnsInLandscape = 3

    // Show Moment
    picker.shouldShowMoment = true

    // Show empty albums
    //        picker.shouldShowMoment = shouldShowEmptyAlbum = true

    // Hide toolbar
    picker.shouldShowEmptyAlbum = true

    // Disable to pick asset
    picker.canPickAsset =  (asset:Asset) -> Bool in
        return false
    

    // Color
    ColorConfig.backgroundColor = UIColor.white
   

【问题讨论】:

【参考方案1】:

使用以下功能

    func getAssetThumbnail(asset: PHAsset) -> UIImage 
    let manager = PHImageManager.default()
    let option = PHImageRequestOptions()
    var thumbnail = UIImage()
    option.version = .original
    option.isSynchronous = true
    manager.requestImage(for: asset, targetSize: CGSize(width: 100, height: 100), contentMode: .aspectFit, options: option, resultHandler: (result, info)->Void in thumbnail = result! )
    return thumbnail

让我们将第一个资产作为

    let phAsset = pickedAssts.first

【讨论】:

以上是关于如何使用 NohanaImagePicker pod 文件查看图像?的主要内容,如果未能解决你的问题,请参考以下文章

如何使用 .po 文件翻译帖子或页面的 Wordpress 内容[关闭]

如何使用新生成的 .pot 文件更新现有的 .po 文件?

cakephp、i18n .po 文件,如何正确使用它们

PO模式你会吗?自动化测试PO模式分层如何实现?

如何使用BADI修改PO

如何生成 .po 文件?