简体   繁体   中英

How can we get images' URLs from Photo Album with Swift 5

I am using following code to get videos' URLs from photo albums of iPhone. Then I can open whichever I want in app but I could not find similar way to do it for images. :(

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

You should use the function requestContentEditingInput to catch URLs indirectly
Your function will be more or less below but I think it will work for you

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM