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)")
}
})
}
}
}
什麼是fetchPhotoAtIndexFromEnd? – mafioso
請注意,你想要的數組可能太大,不能住在內存中 –
好吧,沒有想到這一點。訪問所有照片和視頻的最佳方式是什麼? – mafioso
也許'UIImagePickerController'可以滿足你的需求嗎? –