如何使用Swift从画廊中挑选多个视频以及照片

时间:2018-11-09 04:43:39

标签: 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()
        }
    }
}

2 个答案:

答案 0 :(得分:0)

GalleryGitHub中的开源库,用于从iOS中的照片中选择图像以及视频。

https://github.com/hyperoslo/Gallery

初始化:

GalleryController是主要入口点,只需实例化并赋予它委托即可。

let gallery = GalleryController()
gallery.delegate = self
present(gallery, animated: true, completion: nil)

委托:

GalleryControllerDelegate要求您实现一些委托方法才能与选择器进行交互

func galleryController(_ controller: GalleryController, didSelectImages images: [Image])
func galleryController(_ controller: GalleryController, didSelectVideo video: Video)
func galleryController(_ controller: GalleryController, requestLightbox images: [Image])
func galleryControllerDidCancel(_ controller: GalleryController)

enter image description here

答案 1 :(得分:0)

是的,库的设计不是很好...

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