gpt4 book ai didi

arrays - 我需要创建一个 UIImages 数组,我在 Swift 中从相机胶卷加载这些图像

转载 作者:可可西里 更新时间:2023-11-01 00:38:04 27 4
gpt4 key购买 nike

这是我的代码:

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

关于arrays - 我需要创建一个 UIImages 数组,我在 Swift 中从相机胶卷加载这些图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26951961/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com