收集在两个确切日期之间拍摄的iPhone库照片 [英] Collect Photos of iPhone Library taken between two precise dates

查看:84
本文介绍了收集在两个确切日期之间拍摄的iPhone库照片的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试快速创建一个简单的控制器,使我可以从两个确切日期(例如2015年2月15日,2015年2月18日)之间的照片库中收集照片. 在搜索过程中,我读到了有关iOS的Photo Framework的信息,我想知道是否有一种简单的方法可以根据上述日期使用这种框架查询照片库.我还想获取图像元数据,例如地理位置.如果我可以使用相同的框架来做到这一点,那将是很棒的 感谢您的回答

I'm trying to create a simple controller in swift that allows me to collect photos from the library taken between two precise dates, e.g February the 15th 2015, an February the 18th 2015. During my searches I read about the Photo Framework of iOS, and I'd like to know if there's a simple way to query the photo library with such a framework based on the dates mentioned above. I'd like also to get images metadata like geo location for example. It'd be great if I could do that with the same framework Thanks for your answers

推荐答案

要收集两个日期之间的照片,首先需要创建表示日期范围开始和结束的NSDate.这是一个NSDate扩展名(来自 https://stackoverflow.com/a/24090354/2274694 ),可以创建从其字符串表示形式得出的日期:

To collect the photos between two dates, first you need to create NSDates representing the start and end of the date range. Here's a NSDate extension (from https://stackoverflow.com/a/24090354/2274694) that can create the dates from their string representations:

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

然后使用NSDate s为PHFetchResultPHFetchOptions创建谓词.

Then use the NSDates to create a predicate for the PHFetchResult's PHFetchOptions.

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

这篇关于收集在两个确切日期之间拍摄的iPhone库照片的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆