如何确定PHFetchResult中的PHAsset是否代表已删除的照片? [英] How do I determine if a PHAsset in a PHFetchResult represents a deleted photo?

查看:1239
本文介绍了如何确定PHFetchResult中的PHAsset是否代表已删除的照片?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用iOS 8中的新照片框架抓取设备上拍摄的最后一张照片的缩略图。我现在要执行此操作的代码如下:

I'm trying to grab a thumbnail of the last photo taken on a device using the new Photos framework in iOS 8. The code I have right now to do this is the following:

PHFetchOptions *fetchOptions = [PHFetchOptions new];
fetchOptions.sortDescriptors = @[[NSSortDescriptor sortDescriptorWithKey:@"creationDate" ascending:NO]];
PHFetchResult *assetsInfo = [PHAsset fetchAssetsWithOptions:fetchOptions];

PHImageRequestOptions *requestOptions = [PHImageRequestOptions new];
requestOptions.version = PHImageRequestOptionsVersionCurrent;
requestOptions.deliveryMode = PHImageRequestOptionsDeliveryModeFastFormat;
[[PHImageManager defaultManager] requestImageForAsset:[assetsInfo objectAtIndex:1]
                                           targetSize:CGSizeMake(100, 100)
                                          contentMode:PHImageContentModeAspectFill
                                              options:requestOptions
                                        resultHandler:^(UIImage *result, NSDictionary *info) {
                                            if (result) {
                                                // galleryButton is just a UIButton in the view
                                                [galleryButton setImage:result forState:UIControlStateNormal];
                                            }
                                        }];

代码设法抓取设备上最近拍摄的照片,但是如果拍摄的是最后一张照片设备已删除,未考虑该事实,并且按钮的图像设置为 resultHandler 中已删除的照片。这似乎是因为在iOS 8中,当您删除照片时需要30天才能将其从设备中删除,并且由于某些原因这些照片仍然包含在 PHFetchResult 秒。

The code manages to grab the most recently taken photo on the device, but if the last photo taken on the device was deleted, that fact isn't taken into account, and the button's image is set to the deleted photo in the resultHandler. This seems to be caused by the fact that in iOS 8, when you delete a photo it takes 30 days before it's actually removed from the device, and for some reason these photos are still included in the PHFetchResults.

我试图在 PHAsset 对象中包含的已删除或类似属性c $ c> assetsInfo ,但我找不到任何类型的信息。调试器中已删除照片的打印输出如下所示:

I've tried to look for a "deleted" or similar attribute on the PHAsset objects included in assetsInfo, but I couldn't find any kind of information. The printout of the deleted photo in the debugger looks like this:

<PHAsset: 0x57462a0> 7689FC1C-9EE2-4FF7-9B37-4A032A3FDA01/L0/001 mediaType=1/0, assetSource=2, (1536x2048), creationDate=2014-09-22 06:45:10 +0000, location=1, hidden=0, favorite=0

assetsInfo ,我验证的照片已删除,如下所示:

The printout for the next object in assetsInfo, a photo I verified was not deleted, looks like this:

<PHAsset: 0x57461a0> E48D1482-395B-405C-85F9-FFD04D9EBFBD/L0/001 mediaType=1/0, assetSource=3, (2448x3264), creationDate=2014-09-21 04:24:55 +0000, location=0, hidden=0, favorite=0

似乎没有任何信息可以告诉我这张照片是不是删除。我想也许隐藏属性会有所帮助,但这与照片是否显示在特定的Photos.app相册/集合中有关。我认为也许 assetSource 属性可能有用,但似乎没有记录,经过进一步调查似乎也与删除无关照片的状态。

There doesn't seem to be any information there that can tell me if the photo was deleted. I thought perhaps the hidden attribute would help, but instead it's something to do with whether or not the photo shows up in particular Photos.app albums/collections. I thought that maybe the assetSource attribute might be of use, but it doesn't seem to be documented, and upon further investigation also doesn't seem to be related to the deleted status of the photo.

有没有办法查看 PHAsset 对象是否代表已删除的照片?有没有办法缩小我要提取的资产以排除最近删除的专辑中包含的照片?

Is there a way to see if a PHAsset object represents a deleted photo? Is there a way to narrow down the assets I'm fetching to exclude the photos contained in the "Recently Deleted" album?

推荐答案

PHFetchResult *fetchResults = [PHAsset fetchAssetsWithOptions:options];
NSArray* tempArray = [fetchResults objectsAtIndexes:[NSIndexSet indexSetWithIndexesInRange:NSMakeRange(0, fetchResults.count)]];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"description contains %@",@"assetSource=3"];
NSArray *filteredArray =  [tempArray filteredArrayUsingPredicate:predicate];

filteredArray不包含最近删除的专辑,并且没有相同的照片。同样对于assetsInfo中已删除的照片资产,以下两个将返回NO。

The filteredArray doesn't include the "Recently Deleted" album and doesn't have identical looking photos. Also for the deleted photo asset in your assetsInfo, the following two will return NO.

[asset canPerformEditOperation:PHAssetEditOperationContent] 
[asset canPerformEditOperation:PHAssetEditOperationProperties]

PHAsset Runtime Headers 有一些有用的属性:

@property (getter=isTrashed, nonatomic, readonly) bool trashed;
@property (nonatomic, readonly) NSDate *trashedDate;

这篇关于如何确定PHFetchResult中的PHAsset是否代表已删除的照片?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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