Ios 收集在两个精确日期之间拍摄的iPhone库照片
我正在尝试用swift创建一个简单的控制器,允许我从图书馆收集两个精确日期之间拍摄的照片,例如2015年2月15日和2015年2月18日。 在我的搜索过程中,我读到了有关iOS照片框架的内容,我想知道是否有一种简单的方法可以根据上面提到的日期使用这种框架查询照片库。我还想获得图像元数据,例如地理位置。如果我能用同样的框架来做那就太好了Ios 收集在两个精确日期之间拍摄的iPhone库照片,ios,swift,frameworks,Ios,Swift,Frameworks,我正在尝试用swift创建一个简单的控制器,允许我从图书馆收集两个精确日期之间拍摄的照片,例如2015年2月15日和2015年2月18日。 在我的搜索过程中,我读到了有关iOS照片框架的内容,我想知道是否有一种简单的方法可以根据上面提到的日期使用这种框架查询照片库。我还想获得图像元数据,例如地理位置。如果我能用同样的框架来做那就太好了 感谢您的回答要收集两个日期之间的照片,首先需要创建表示日期范围开始和结束的日期。以下是一个NSDate扩展,可以从其字符串表示形式创建日期: extension
感谢您的回答要收集两个日期之间的照片,首先需要创建表示日期范围开始和结束的日期。以下是一个NSDate扩展,可以从其字符串表示形式创建日期:
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)
}
}
然后使用NSDates为PHFetchResult的PHFetchOptions创建谓词
import UIKit
import Photos
class ViewController: UIViewController {
var images:[UIImage] = [] // <-- Array to hold the fetched images
override func viewDidLoad() {
super.viewDidLoad()
fetchPhotosInRange(startDate: NSDate(dateString:"07-15-2018"), endDate: NSDate(dateString:"07-31-2018"))
}
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 = []
if 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 {
// for var index = 0 ; index < fetchResult.count ; index++ {
let asset = fetchResult.object(at: index)
// Request Image
imgManager.requestImageData(for: asset, options: requestOptions, resultHandler: { (imageData, str, orientation, info) -> 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("images ==>\(images)")
}
为Swift 3更新:
首先,我要感谢“Lyndsey Scott”,感谢她编写了如此出色的代码。这真的很有帮助。可能会向少数编译器返回错误,因为这些是最新的,代码需要更新一点。因此,这里我给出了最新更新的代码,以使Lyndsey的代码在最新的Swift 4.0或更高版本编译器中无错误
extension NSDate {
convenience
init(dateString:String) {
let dateStringFormatter = DateFormatter()
dateStringFormatter.dateFormat = "MM-dd-yyyy"
dateStringFormatter.locale = NSLocale(localeIdentifier: "en_US_POSIX") as Locale?
let d = dateStringFormatter.date(from: dateString)!
self.init(timeInterval: 0, since: d)
}
}
然后使用NSDates为PHFetchResult的PHFetchOptions创建谓词
import UIKit
import Photos
class ViewController: UIViewController {
var images:[UIImage] = [] // <-- Array to hold the fetched images
override func viewDidLoad() {
super.viewDidLoad()
fetchPhotosInRange(startDate: NSDate(dateString:"07-15-2018"), endDate: NSDate(dateString:"07-31-2018"))
}
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 = []
if 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 {
// for var index = 0 ; index < fetchResult.count ; index++ {
let asset = fetchResult.object(at: index)
// Request Image
imgManager.requestImageData(for: asset, options: requestOptions, resultHandler: { (imageData, str, orientation, info) -> 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("images ==>\(images)")
}
快乐编码..你调查过吗?@T先生,答案已经过时了。现在最好用照片框架代替资产库框架。@Lindsey该死的,我需要睡一觉。。。在我打开的所有文章中感到困惑,所以在Swift 3中出现了大量错误。也许你需要更新answer@GeorgeAsda我将代码更新为Swift 3,但没有看到任何错误。。。您指的是哪些错误?前面的代码段在Swift 3中抛出了错误。我没有测试过的更新版本。@GeorgeAsda它们是错误吗?也就是说,您是否自己转换代码、运行代码,以及构建是否产生错误?或者你只是说代码不会编译,因为它是Swift的另一个版本?您必须将swift代码的任何版本转换为您正在使用的swift的任何版本,如果这是您的意思。。。否则代码将不兼容。。。
import UIKit
import Photos
class ViewController: UIViewController {
var images:[UIImage] = [] // <-- Array to hold the fetched images
override func viewDidLoad() {
super.viewDidLoad()
fetchPhotosInRange(startDate: NSDate(dateString:"07-15-2018"), endDate: NSDate(dateString:"07-31-2018"))
}
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 = []
if 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 {
// for var index = 0 ; index < fetchResult.count ; index++ {
let asset = fetchResult.object(at: index)
// Request Image
imgManager.requestImageData(for: asset, options: requestOptions, resultHandler: { (imageData, str, orientation, info) -> 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("images ==>\(images)")
}