使用照片框架按图片大小过滤照片库
Filtering photo library by picture size with Photos framework
我正在构建 iOS 应用程序以从用户的照片库中选择屏幕截图。该应用程序有效,除了我可以显示所有照片并且我只需要屏幕截图。为了检测屏幕截图,我想将图像大小与屏幕大小相匹配。
var userScreenshots: PHFetchResult!
let screenWidth = view.bounds.size.width * 2
let screenHeight = view.bounds.size.height * 2
let options = PHFetchOptions()
// Following line needs to be changed —>
options.predicate = NSPredicate(format: "(pixelWidth & %d) != 0 || (pixelHeight & %d) != 0", screenWidth, screenHeight)
options.sortDescriptors = [NSSortDescriptor(key: "creationDate", ascending: true)]
userScreenshots = PHAsset.fetchAssetsWithOptions(options)
崩溃:
2015-06-22 23:00:24.540 Altershot[95046:24515142] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Unsupported predicate in fetch options: pixelWidth & 0 != 0'
请解释如何正确定义这个谓词。
这是正确的代码。请注意,这不是幻数 2
,而是 scale
通用 属性,因为 iPhone 6 Plus 的规模为 3
。我还包括横向屏幕截图,感谢您的评论。此外,改进了屏幕尺寸公式,为 iOS 9 多任务处理做好准备。
var userScreenshots: PHFetchResult!
let scale = UIScreen.mainScreen().scale
let screenWidth = UIScreen.mainScreen().bounds.width * scale
let screenHeight = UIScreen.mainScreen().bounds.height * scale
let options = PHFetchOptions()
options.predicate = NSPredicate(format: "(pixelHeight == %d AND pixelWidth == %d) OR (pixelHeight == %d AND pixelWidth == %d)", argumentArray: [screenHeight, screenWidth, screenWidth, screenHeight])
options.sortDescriptors = [NSSortDescriptor(key: "creationDate", ascending: true)]
userScreenshots = PHAsset.fetchAssetsWithOptions(options)
我正在构建 iOS 应用程序以从用户的照片库中选择屏幕截图。该应用程序有效,除了我可以显示所有照片并且我只需要屏幕截图。为了检测屏幕截图,我想将图像大小与屏幕大小相匹配。
var userScreenshots: PHFetchResult!
let screenWidth = view.bounds.size.width * 2
let screenHeight = view.bounds.size.height * 2
let options = PHFetchOptions()
// Following line needs to be changed —>
options.predicate = NSPredicate(format: "(pixelWidth & %d) != 0 || (pixelHeight & %d) != 0", screenWidth, screenHeight)
options.sortDescriptors = [NSSortDescriptor(key: "creationDate", ascending: true)]
userScreenshots = PHAsset.fetchAssetsWithOptions(options)
崩溃:
2015-06-22 23:00:24.540 Altershot[95046:24515142] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Unsupported predicate in fetch options: pixelWidth & 0 != 0'
请解释如何正确定义这个谓词。
这是正确的代码。请注意,这不是幻数 2
,而是 scale
通用 属性,因为 iPhone 6 Plus 的规模为 3
。我还包括横向屏幕截图,感谢您的评论。此外,改进了屏幕尺寸公式,为 iOS 9 多任务处理做好准备。
var userScreenshots: PHFetchResult!
let scale = UIScreen.mainScreen().scale
let screenWidth = UIScreen.mainScreen().bounds.width * scale
let screenHeight = UIScreen.mainScreen().bounds.height * scale
let options = PHFetchOptions()
options.predicate = NSPredicate(format: "(pixelHeight == %d AND pixelWidth == %d) OR (pixelHeight == %d AND pixelWidth == %d)", argumentArray: [screenHeight, screenWidth, screenWidth, screenHeight])
options.sortDescriptors = [NSSortDescriptor(key: "creationDate", ascending: true)]
userScreenshots = PHAsset.fetchAssetsWithOptions(options)