如何将相机胶卷中的所有照片和视频插入阵列?

时间:2016-07-26 09:21:10

标签: ios arrays swift camera-roll

有没有办法将相机胶卷中的所有内容添加到阵列?

1 个答案:

答案 0 :(得分:0)

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