收集在两个确切date之间拍摄的iPhone库的照片

我试图在swift中创build一个简单的控制器,使我可以从两个精确的date之间采集的图库中收集照片,例如2015年2月15日,2015年2月18日。在我的search过程中,我阅读了iOS的Photo Framework,我想知道是否有一个简单的方法来查询照片库的基础上述date这样的框架。 我也想获取像地理位置的图像元数据。 如果我可以用相同的框架做到这一点,这将是非常好的感谢您的答案

要在两个date之间收集照片,首先需要创build代表date范围的开始和结束的NSDate 。 这是一个NSDate扩展(从https://stackoverflow.com/a/24090354/2274694 ),可以从他们的string表示中创builddate:

 extension NSDate { convenience init(dateString:String) { let dateStringFormatter = NSDateFormatter() dateStringFormatter.dateFormat = "MM-dd-yyyy" dateStringFormatter.locale = NSLocale(localeIdentifier: "en_US_POSIX") let d = dateStringFormatter.dateFromString(dateString)! self.init(timeInterval:0, sinceDate:d) } } 

然后使用NSDatePHFetchResultPHFetchOptions创build一个谓词。

 import Photos class ViewController: UIViewController { var images:[UIImage] = [] // <-- Array to hold the fetched images override func viewDidLoad() { fetchPhotosInRange(NSDate(dateString:"04-06-2015"), endDate: NSDate(dateString:"04-16-2015")) } func fetchPhotosInRange(startDate:NSDate, endDate:NSDate) { let imgManager = PHImageManager.defaultManager() let requestOptions = PHImageRequestOptions() requestOptions.synchronous = true requestOptions.networkAccessAllowed = true // Fetch the images between the start and end date let fetchOptions = PHFetchOptions() fetchOptions.predicate = NSPredicate(format: "creationDate > %@ AND creationDate < %@", startDate, endDate) images = [] if let fetchResult: PHFetchResult = PHAsset.fetchAssetsWithMediaType(PHAssetMediaType.Image, options: fetchOptions) { // If the fetch result isn't empty, // proceed with the image request if fetchResult.count > 0 { // Perform the image request for var index = 0 ; index < fetchResult.count ; index++ { let asset = fetchResult.objectAtIndex(index) as! PHAsset imgManager.requestImageDataForAsset(asset, options: requestOptions, resultHandler: { (imageData: NSData?, dataUTI: String?, orientation: UIImageOrientation, info: [NSObject : AnyObject]?) -> Void in if let imageData = imageData { if let image = UIImage(data: imageData) { // Add the returned image to your array self.images += [image] } } if self.images.count == fetchResult.count { // Do something once all the images // have been fetched. (This if statement // executes as long as all the images // are found; but you should also handle // the case where they're not all found.) } }) } } } } } 

更新了Swift 3:

 import UIKit import Photos class ViewController: UIViewController { var images:[UIImage] = [] // <-- Array to hold the fetched images override func viewDidLoad() { let formatter = DateFormatter() formatter.dateFormat = "MM-dd-yyyy" fetchPhotosInRange(startDate: formatter.date(from: "04-06-2015")! as NSDate, endDate: formatter.date(from: "04-16-2015")! as NSDate) } func fetchPhotosInRange(startDate:NSDate, endDate:NSDate) { let imgManager = PHImageManager.default() let requestOptions = PHImageRequestOptions() requestOptions.isSynchronous = true requestOptions.isNetworkAccessAllowed = true // Fetch the images between the start and end date let fetchOptions = PHFetchOptions() fetchOptions.predicate = NSPredicate(format: "creationDate > %@ AND creationDate < %@", startDate, endDate) images = [] 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 { // Perform the image request for index in 0 ..< fetchResult.count { let asset = fetchResult.object(at: index) imgManager.requestImageData(for: asset, options: requestOptions, resultHandler: { (imageData: Data?, dataUTI: String?, orientation: UIImageOrientation, info: [AnyHashable : Any]?) -> Void in if let imageData = imageData { if let image = UIImage(data: imageData) { // Add the returned image to your array self.images += [image] } } if self.images.count == fetchResult.count { // Do something once all the images // have been fetched. (This if statement // executes as long as all the images // are found; but you should also handle // the case where they're not all found.) print(self.images) } }) } } } }