gpt4 book ai didi

ios - 使用 Swift 3 将图像文件名存储到数组中

转载 作者:可可西里 更新时间:2023-11-01 02:03:44 26 4
gpt4 key购买 nike

我正在编写一个具有屏幕保护程序的应用程序,该屏幕保护程序遍历 iPad 相机胶卷中的照片。我坚持将图像文件名存储到一个数组中,以便我可以相应地显示图像。

import Photos

@IBOutlet weak var ssImage: UIImageView!
let fetchOptions = PHFetchOptions()
var arrayPhoto: [UIImage] = []

func FetchPhotos() {
let allPhotos = PHAsset.fetchAssets(with: .image, options: fetchOptions)

//Get the file names of all the photos and store into arrayPhoto
}

//Gets index of current image from array and increment 1 so as to display the next photo every 5 seconds.
func nextImage() {
let currentIndex = arrayPhoto.index(of: imageView.image ?? UIImage()) ?? -1
var nextIndex = currentIndex+1
nextIndex = arrayPhoto.indices.contains(nextIndex) ? nextIndex : 0
UIView.transition(with: ssImage, duration: 0.5, options: .transitionCrossDissolve, animations: { self.ssImage.image = self.arrayPhoto[nextIndex] }, completion: nil)
ssImage.image = arrayPhoto[nextIndex]
}

func scheduledTimer() {
timer = Timer.scheduledTimer(timeInterval: 5, target: self, selector: (#selector(nextImage)), userInfo: nil, repeats: true)
}

override func viewDidLoad() {
super.viewDidLoad
FetchPhotos()
scheduledTimer()
}

最佳答案

我收到你的问题了。现在创建函数 convertAssetToImage

func convertAssetToImage(asset: [PHAsset], completion: (_ objects: [UIImage]) -> Void) {
var result: [UIImage] = []
asset.forEach { (item) in

imageManager.requestImage(for: item, targetSize: CGSize(width: item.pixelWidth, height: item.pixelHeight), contentMode: .aspectFill, options: nil, resultHandler: { image, _ in
// The cell may have been recycled by the time this handler gets called;
// set the cell's thumbnail image only if it's still showing the same asset.
result.append(image!)
})

}
completion(result)
}

用你的函数调用它

func FetchPhotos() {
let photoAssets = PHAsset.fetchAssets(with: .image, options: fetchOptions)
convertAssetToImage(asset: photoAssets, completion: { (images) in
arrayPhoto = images
})
}

注意:调用 FetchPhotos() 函数给 viewDidLoad 时要小心,因为 func convertAssetToImage 是异步函数。

关于ios - 使用 Swift 3 将图像文件名存储到数组中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44689368/

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