OStack程序员社区-中国程序员成长平台

标题: ios - 如何使用 Swift 从图库中挑选多个视频和照片 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-11 20:47
标题: 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()
        }
    }
}



Best Answer-推荐答案


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

这里,刚出炉: 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/






欢迎光临 OStack程序员社区-中国程序员成长平台 (https://ostack.cn/) Powered by Discuz! X3.4