Is there a way to add everything from your camera roll to an array?
Asked
Active
Viewed 58 times
0
-
2note that array you wanted can be too big to live in memory – Yury Jul 26 '16 at 09:24
-
oh ok, didn't think of that. What is the best way to have access to all photos and videos? – mafioso Jul 26 '16 at 09:28
-
maybe `UIImagePickerController` can cover your needs? – Yury Jul 26 '16 at 09:30
-
use this link http://stackoverflow.com/questions/12633843/get-all-of-the-pictures-from-an-iphone-photolibrary-in-an-array-using-assetslibr – vikrant tanwar Jul 26 '16 at 09:35
-
no I don't want to use the UIImagePickerController. I want them to appear automatically without doing anything – mafioso Jul 26 '16 at 09:38
1 Answers
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)")
}
})
}
}
}

Harris
- 310
- 3
- 13