簡體   English   中英

如何將相機膠卷中的所有照片和視頻插入陣列?

[英]How to insert all photos and videos from camera roll into array?

有沒有一種方法可以將相機膠卷中的所有內容添加到陣列中?

import Photos

class MyVC: UIViewController 
{
var images:NSMutableArray! // Array for storing images


func fetchPhotos () {
    images = NSMutableArray()
    self.fetchPhotoAtIndexFromEnd(0)
}


func getImageAtIndex(index:Int) {

    let imgManager = PHImageManager.defaultManager()


    var requestOptions = PHImageRequestOptions()
    requestOptions.synchronous = true // if just return thumbnail not img

    // optionally sort the images by creation date
    var fetchOptions = PHFetchOptions()
    fetchOptions.sortDescriptors = [NSSortDescriptor(key:"creationDate", ascending: false)]

    if let fetchResult: PHFetchResult = PHAsset.fetchAssetsWithMediaType(PHAssetMediaType.Image, options: fetchOptions) {


        if fetchResult.count > 0 {
            imgManager.requestImageForAsset(fetchResult.objectAtIndex(fetchResult.count - 1 - index) as PHAsset, targetSize: view.frame.size, contentMode: PHImageContentMode.AspectFill, options: requestOptions, resultHandler: { (image, _) in

                // Add img to images array
                self.images.addObject(image)


                if index + 1 < fetchResult.count {
                    self.fetchPhotoAtIndexFromEnd(index + 1)
                } else {

                    println("Completed array: \(self.images)")
                }
            })
        }
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM