久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

Swift - 如何從照片庫中獲取最后拍攝的 3 張照片

Swift - how to get last taken 3 photos from photo library?(Swift - 如何從照片庫中獲取最后拍攝的 3 張照片?)
本文介紹了Swift - 如何從照片庫中獲取最后拍攝的 3 張照片?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

我需要在 viewDidload 事件中從照片庫中獲取并顯示最后拍攝的 3 張照片,而無需任何點擊.

I need to get and show last taken 3 photos from photo library on viewDidload event without any clicks.

在這一步之后,當我滾動 ScrollView 時,我應該得到其他 3 張 3 的照片.

After this step, I should get other photos 3 by 3 when I scroll the ScrollView.

您知道使用 swift 執行此操作的正確方法嗎?謝謝.

Do you know the proper way to do this with swift? Thanks.

推薦答案

這是一個使用 iOS 8+ 設備可用的 Photos 框架的解決方案:

Here's a solution using the Photos framework available for devices iOS 8+ :

import Photos

class ViewController: UIViewController {

    var images:[UIImage] = []

    func fetchPhotos () {
        // Sort the images by descending creation date and fetch the first 3
        let fetchOptions = PHFetchOptions()
        fetchOptions.sortDescriptors = [NSSortDescriptor(key:"creationDate", ascending: false)]
        fetchOptions.fetchLimit = 3

        // Fetch the image assets
        let fetchResult: PHFetchResult = PHAsset.fetchAssets(with: PHAssetMediaType.image, options: fetchOptions)

        // If the fetch result isn't empty,
        // proceed with the image request
        if fetchResult.count > 0 {
            let totalImageCountNeeded = 3 // <-- The number of images to fetch
            fetchPhotoAtIndex(0, totalImageCountNeeded, fetchResult)
        }
    }

    // Repeatedly call the following method while incrementing
    // the index until all the photos are fetched
    func fetchPhotoAtIndex(_ index:Int, _ totalImageCountNeeded: Int, _ fetchResult: PHFetchResult<PHAsset>) {

        // Note that if the request is not set to synchronous
        // the requestImageForAsset will return both the image
        // and thumbnail; by setting synchronous to true it
        // will return just the thumbnail
        let requestOptions = PHImageRequestOptions()
        requestOptions.isSynchronous = true

        // Perform the image request
        PHImageManager.default().requestImage(for: fetchResult.object(at: index) as PHAsset, targetSize: view.frame.size, contentMode: PHImageContentMode.aspectFill, options: requestOptions, resultHandler: { (image, _) in
            if let image = image {
                // Add the returned image to your array
                self.images += [image]
            }
            // If you haven't already reached the first
            // index of the fetch result and if you haven't
            // already stored all of the images you need,
            // perform the fetch request again with an
            // incremented index
            if index + 1 < fetchResult.count && self.images.count < totalImageCountNeeded {
                self.fetchPhotoAtIndex(index + 1, totalImageCountNeeded, fetchResult)
            } else {
                // Else you have completed creating your array
                print("Completed array: (self.images)")
            }
        })
    }
}

這篇關于Swift - 如何從照片庫中獲取最后拍攝的 3 張照片?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

How to subclass UIScrollView and make the delegate property private(如何繼承 UIScrollView 并使委托屬性私有)
Setting contentOffset programmatically triggers scrollViewDidScroll(以編程方式設置 contentOffset 觸發 scrollViewDidScroll)
Photos app-like gap between pages in UIScrollView with pagingEnabled(使用 pagingEnabled 的 UIScrollView 中頁面之間的照片應用程序式間隙)
why UIScrollView is leaving space from top in ios 6 and ios 7(為什么 UIScrollView 在 ios 6 和 ios 7 中從頂部留下空間)
UIScrollView pauses NSTimer while scrolling(UIScrollView 在滾動時暫停 NSTimer)
Limiting the scrollable area in UIScrollView(限制 UIScrollView 中的可滾動區域)
主站蜘蛛池模板: 亚洲一区国产 | 一区二区三区欧美大片 | 欧美性另类 | 成人免费视频一区二区 | 久久一区 | 久久91| 视频一区在线播放 | 黄视频网站在线 | 亚洲国产精品久久久久 | 国产精品久久久久久婷婷天堂 | 国产日韩一区二区三区 | 激情五月婷婷 | 久久精品视频免费观看 | 久久久久亚洲精品 | 最近日韩中文字幕 | 黄色大全免费看 | 一区二区三区四区免费在线观看 | 成人在线视频免费观看 | 亚洲视频国产 | 欧美日韩国产一区 | 亚洲一区二区精品视频在线观看 | 一区视频在线 | 伊人久久综合 | 亚洲欧美日韩中文字幕一区二区三区 | 中文字幕动漫成人 | 欧美色图综合网 | 色综合久久久 | 久久99精品久久久久久 | 国产一区二区三区 | 亚洲精品乱码久久久久久久久久 | 99久久久99久久国产片鸭王 | 国产在线97| 亚洲欧洲在线观看视频 | jizz视频| 成人妇女免费播放久久久 | 中文字幕日韩一区 | 免费特黄视频 | 亚洲最大福利网 | 综合天天久久 | 中文字幕久久精品 | 一区亚洲|