我需要创建一个UIImages数组,我从Swift中的Camera roll加载

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了我需要创建一个UIImages数组,我从Swift中的Camera roll加载相关的知识,希望对你有一定的参考价值。

这是我的代码:

import UIKit
import Photos //Photos.h del framework Photos
import MediaPlayer
import AssetsLibrary

let reuseIdentifier = "PhotoCell"
let albumName = "My App"

class PhotosViewController: UIViewController, UICollectionViewDataSource,    UICollectionViewDelegate, UICollectionViewDelegateFlowLayout,        UIImagePickerControllerDelegate, UINavigationControllerDelegate {

var assetCollection:PHAssetCollection!
var photoAsset:PHFetchResult!
var imageManager = PHImageManager.defaultManager()
var albumFound:Bool = false

@IBOutlet weak var collectionView: UICollectionView!

@IBAction func creaFilmato(sender: AnyObject) {

    println("Crea il tuo filmato")

    /* With a static array works perfectly
    let img1:UIImage = UIImage(named: "image1.jpg")!
    let img2:UIImage = UIImage(named: "image2.jpg")!
    let img3:UIImage = UIImage(named: "image3.jpg")!

    var array:Array<UIImage> = [img1,img2,img3]*/

    let img1:UIImage = UIImage(named: "image1.jpg")!

    let settings:NSDictionary = CEMovieMaker.videoSettingsWithCodec(AVVideoCodecH264, withWidth: img1.size.width, andHeight: img1.size.height)
    let prova = CEMovieMaker(settings: settings)

    prova.createMovieFromImages([self.photoAsset.copy()], withCompletion: {(success:Bool, fileURL:NSURL!) in

        if(success){

            self.viewMovieAtURL(fileURL)

            /*// Salvo il filmato in cameraRoll

            let library:ALAssetsLibrary = ALAssetsLibrary()

            library.writeVideoAtPathToSavedPhotosAlbum(fileURL, completionBlock: {(assetURL:NSURL!, error) in

            if((error) != nil) {

            println("Errore nel salvataggio del filmato %@",error)
            }

            })*/
        }

    })

    println("Esportato!")

}

func viewMovieAtURL(fileURL:NSURL!)->Void {

    let playerController:MPMoviePlayerViewController = MPMoviePlayerViewController(contentURL: fileURL!)

    playerController.view.frame = self.view.bounds
    self.presentMoviePlayerViewControllerAnimated(playerController)
    playerController.moviePlayer.prepareToPlay()
    playerController.moviePlayer.play()

    self.view.addSubview(playerController.view)

}

在我看来,错误的代码是[self.photoAsset.copy()]因为photoAsset返回PHFetchResult而不是UIImage数组。从Swift中的PHFetchResult开始获取图像数组的代码是什么?

答案
var images = [UIImage]()
let targetSize: CGSize = // your target size
let contentMode: PHImageContentMode = // your content mode

// photoAsset is an object of type PHFetchResult
photoAsset.enumerateObjectsUsingBlock {
        object, index, stop in

        let options = PHImageRequestOptions()
        options.synchronous = true
        options.deliveryMode = .HighQualityFormat

        PHImageManager.defaultManager().requestImageForAsset(object as PHAsset, targetSize: targetSize, contentMode: contentMode, options: options) {
                image, info in
                images.append(image)
        }
}
另一答案

这就是我使用Swift 4.2的方法

let fetchOptions = PHFetchOptions()
let allPhotos = PHAsset.fetchAssets(with: .image, options: fetchOptions)
let targetSize = CGSize(width: 100, height: 100) // your target size

allPhotos.enumerateObjects {
    object, index, stop in
    var thumbnail = UIImage()
    let options = PHImageRequestOptions()
    options.isSynchronous = true
    options.deliveryMode = .highQualityFormat
    PHImageManager.default().requestImage(for: object, targetSize: targetSize, contentMode: .aspectFill, options: options, resultHandler: { image, _ in
        thumbnail = image!
    })
}

希望这有帮助!

以上是关于我需要创建一个UIImages数组,我从Swift中的Camera roll加载的主要内容,如果未能解决你的问题,请参考以下文章

如何在 xCode 中保存 UIimages 数组(swift)

如何将数组复制到 UIImages 到另一个数组中?

如何创建 UIImages 数组

存储 UIImages 的持久数组 - CoreData 还是在磁盘上?

如何通过按 UIButton 从数组中切换 UIViewController 中的 UIImages

如何对 UIImages 数组进行排序?