Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Arrays 我需要创建一个UIImages数组,从Swift中的摄影机滚动加载_Arrays_Swift_Uiimage_Ios8 - Fatal编程技术网

Arrays 我需要创建一个UIImages数组,从Swift中的摄影机滚动加载

Arrays 我需要创建一个UIImages数组,从Swift中的摄影机滚动加载,arrays,swift,uiimage,ios8,Arrays,Swift,Uiimage,Ios8,下面是我的代码: import UIKit import Photos //Photos.h del framework Photos import MediaPlayer import AssetsLibrary let reuseIdentifier = "PhotoCell" let albumName = "My App" class PhotosViewController: UIViewController, UICollectionViewDataSource, UICo

下面是我的代码:

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)

}
导入UIKit
导入照片//照片.h del framework照片
导入MediaPlayer
导入资产库
让reuseIdentifier=“光电管”
让albumName=“我的应用程序”
类PhotoViewController:UIViewController、UICollectionViewDataSource、UICollectionViewDelegate、UICollectionViewDelegateFlowLayout、UIImagePickerController、UINavigationControllerDelegate{
var assetCollection:PhaseSetCollection!
var photoAsset:PHFetchResult!
var imageManager=PHImageManager.defaultManager()
变量:Bool=false
@ibvar collectionView:UICollectionView!
@IBAction func火葬场(发件人:AnyObject){
println(“Crea il tuo filmato”)
/*使用静态数组可以完美地工作
让img1:UIImage=UIImage(名为:“image1.jpg”)!
让img2:UIImage=UIImage(名为:“image2.jpg”)!
让img3:UIImage=UIImage(名为:“image3.jpg”)!
变量数组:数组=[img1、img2、img3]*/
让img1:UIImage=UIImage(名为:“image1.jpg”)!
let settings:NSDictionary=CEMovieMaker.videoSettingsWithCodec(AVVideoCodecH264,宽度:img1.size.width,高度:img1.size.height)
设prova=CEMovieMaker(设置:设置)
prova.createMovieFromImages([self.photoAsset.copy()],在中完成:{(成功:Bool,文件URL:NSURL!)
如果(成功){
self.viewMovieAtURL(文件URL)
/*//卡梅罗里的萨尔沃·伊尔·菲马托
let library:ALAssetsLibrary=ALAssetsLibrary()
library.writeVideoAtPathToSavedPhotosAlbum(fileURL,completionBlock:{(assetURL:NSURL!,错误)位于
如果((错误)!=nil){
println(“Errore nel salvataggio del filmato%@”,错误)
}
})*/
}
})
println(“Esportato!”)
}
func VIEWMOVIATURL(文件URL:NSURL!)->无效{
让playerController:MPMoviePlayerViewController=MPMoviePlayerViewController(contentURL:fileURL!)
playerController.view.frame=self.view.bounds
self.presentmovieplayervewcontrolleranimated(playerController)
playerController.moviePlayer.prepareToPlay()
playerController.moviePlayer.play()
self.view.addSubview(playerController.view)
}

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

这是我使用Swift 4.2的方法

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)
        }
}
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!
    })
}
希望这有帮助