首页 > 解决方案 > How to pick multiple videos as well as photos from gallery using Swift

问题描述

I'm trying to pick both photos and videos from gallery.

I have done a few things like picked photos but not showing the videos. I have used BSImagepicker library and I have't found any video related thing in this library. If anyone have done such kind of work or know about any other library which picks both photos and videos please refer.

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()
        }
    }
}

标签: iosswift

解决方案


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

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

推荐阅读