gpt4 book ai didi

ios - 如何使用Swift 5从相册获得图像的URL

转载 作者:行者123 更新时间:2023-12-01 19:29:20 24 4
gpt4 key购买 nike

我正在使用以下代码从iPhone的相册中获取视频的URL。
然后我可以在应用程序中打开任何我想要的
我找不到用于图像的类似方法。 :(

var videoList: [String] = []
var videoListURL: [NSURL] = []

// Gets all the video file list from phone library
func fetchAllVideos() {
videoListURL.removeAll()
videoList.removeAll()
let fetchOptions = PHFetchOptions()

fetchOptions.predicate = NSPredicate(format: "mediaType = %d ", PHAssetMediaType.video.rawValue )

let allVideo = PHAsset.fetchAssets(with: .video, options: fetchOptions)

allVideo.enumerateObjects { (asset, index, bool) in

let imageManager = PHCachingImageManager()
let options = PHVideoRequestOptions()
options.isNetworkAccessAllowed = false
imageManager.requestAVAsset(forVideo: asset,
options: options,
resultHandler: { [self] (video, audioMix, info) in

guard let video2 = video as? AVURLAsset
else {return}


let urlVideo = video2.url

videoListURL.append(urlVideo as NSURL)
var names = urlVideo.lastPathComponent
if names.contains("MOV") {
names.removeLast(4)
self.videoList.append(names)
}


})


}
}

最佳答案

您应该使用requestContentEditingInput函数间接捕获URL
您的功能或多或少会在下面,但我认为它将为您工作

import Photos
import UIKit

var ImageList: [UIImage] = []
var ImageListURL: [URL] = []

func getPhotos() {

let requestOptions = PHImageRequestOptions()
let manager = PHImageManager.default()
requestOptions.isSynchronous = false
requestOptions.deliveryMode = .highQualityFormat

let fetchOptions = PHFetchOptions()
fetchOptions.sortDescriptors = [NSSortDescriptor(key: "Url", ascending: false)]

let results: PHFetchResult = PHAsset.fetchAssets(with: .image, options: fetchOptions)

results.enumerateObjects { (asset, index, bool) in

if results.count > 0 {
for i in 0..<results.count {
let asset = results.object(at: i)
let size = CGSize(width: 700, height: 700)

let contentEditing = PHContentEditingInputRequestOptions()

asset.requestContentEditingInput(with: contentEditing) { (contentInput, dic) in
guard let assetUrl = contentInput?.fullSizeImageURL else { return }
ImageListURL.append(assetUrl)
print("\(index):\(assetUrl)")
}

manager.requestImage(for: asset, targetSize: size, contentMode: .aspectFill, options: requestOptions) { (image, _) in
if let image = image {
ImageList.append(image)

} else {
print("Error for Url")
}
}
}
} else {
print("No photos in your library")
}
}
}

关于ios - 如何使用Swift 5从相册获得图像的URL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64076879/

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