我们如何使用 Swift 5 从相册中获取图像的 URL
How can we get images' URLs from Photo Album with Swift 5
我正在使用以下代码从 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()
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")
}
}
}
我正在使用以下代码从 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()
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")
}
}
}