ios - 如何使用 Swift 从图库中选择多个视频和照片

标签 ios swift

我正在尝试从图库中选择照片和视频。

我做了一些事情,比如挑选照片但没有显示视频。我使用过 BSImagepicker 库,但我在这个库中没有找到任何与视频相关的东西。如果有人做过此类工作或知道任何其他同时挑选照片和视频的图书馆,请引用。

import UIKit
import Photos
import BSImageView
import BSImagePicker

class ViewController: UIViewController {
    let pickerController = BSImagePickerViewController()
    var SelectedAssets = [PHAsset]()
    var PhotoArray = [UIImage]()
    @IBOutlet var imageView: UIImageView!

    @IBAction func buttonAction(_ sender: Any) {
        pickImages()
    }

    func pickImages() {
        self.bs_presentImagePickerController(pickerController, animated: true, select: { (asset: PHAsset) in
        }, deselect: { (assets: PHAsset) in
        }, cancel: { (assets: [PHAsset]) in
        }, finish: { (assets: [PHAsset]) in
            for i in 0..<assets.count {
                self.SelectedAssets.append(assets[i])
            }
            self.convertAssetToImages()
        }, completion: nil)
    }

    func convertAssetToImages(){
        if SelectedAssets.count != 0 {
            for i in 0..<SelectedAssets.count {
                let manager = PHImageManager.default()
                let option = PHImageRequestOptions()
                var thumbnail = UIImage()
                option.isSynchronous = true
                manager.requestImage(for: SelectedAssets[i], targetSize: CGSize(width: 500, height: 500), contentMode: .aspectFit, options: option, resultHandler: { (result, info) in
                    thumbnail = result!
                })
                let data = UIImageJPEGRepresentation(thumbnail, 0.7)
                let newImage = UIImage(data: data!)
                self.PhotoArray.append(newImage! as UIImage)
            }
            hcArray = PhotoArray
            self.imageView.animationImages = self.PhotoArray
            self.imageView.animationDuration = 3.0
            self.imageView.startAnimating()
        }
    }
}

最佳答案

你是对的,图书馆的设计不是很好......

这里,刚出炉的: https://cocoapods.org/pods/CameraRoll

我将添加一个自述文件并对其进行改进,但它运行良好。

首先,将它添加到你的plist

<key>NSPhotoLibraryUsageDescription</key>
<string>To Find images</string>

然后导入

import CameraRoll
import Photos

然后当你想使用它的时候,

   let cr = CameraRoll()
    // For pitures use .Image mode
    cr.present(in: self, mode: .Video) { (assets) in
        // Returns array of PHAsset
        }
    }

使用辅助函数进行转换

    let cr = CameraRoll()
    cr.present(in: self, mode: .Image) { (assets) in
        if let assets = assets, let first = assets.first {
            DispatchQueue.main.async {
                AssetManager.sharedInstance.getImageFromAsset(phAsset: first) { (image) in
                    self.imageView.image = image
                }
            }
        }
    }

关于ios - 如何使用 Swift 从图库中选择多个视频和照片,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53219986/

相关文章:

ios - 在 iPhoneSDK 中为订阅创建多少产品 ID

iOS App 不会使用自定义 iCloud 容器

arrays - 迭代数组并计数从负到正的变化(反之亦然)

ios - 如何在 swift 应用程序中获取 keyWindow 引用?

iphone - 如何创建下拉框?

objective-c - 无法加载localizable.strings

ios - 为什么滑动手势识别器只能工作一次?

ios - Collection View 、数组和字典

ios - 无法在 Objective-C 中将 Swift 类与枚举一起使用

xcode - viewDidLoad() 之后出现意外的打印对话框