我尝试从照片库中获取两个日期范围内的图像。
首先,我以字典的形式一张一张地获取照片库图像的信息,并使用键选择每个图像日期,并使用if条件将该日期与两个日期进行比较。
如果该图像的日期介于两个日期之间,则将该图像插入数组。
我要以阵列形式保存图像,因为我想在集合视图中显示它们。
当它在模拟器上运行时,由于内存问题,它不能在实际设备上运行。
我认为真实设备照片库中有一堆图像,这就是为什么出现内存问题。
我怎么解决这个问题?
根据我们在您同意切换到Photos Framework而不是Assets Library的注释中的对话,与其将图像保存到阵列中,还不将PHAsset的本地标识符保存到阵列中。
为了按日期获取图像,为了可重用性,首先创建一个实用程序方法来创建日期:
-(NSDate*) getDateForDay:(NSInteger) day andMonth:(NSInteger) month andYear:(NSInteger) year{
NSDateComponents *comps = [[NSDateComponents alloc] init];
[comps setDay:day];
[comps setMonth:month];
[comps setYear:year];
NSDate *date = [[NSCalendar currentCalendar] dateFromComponents:comps];
return date;
}
您可以这样创建startDate和endDate:
NSDate *startDate = [self getDateForDay:11 andMonth:10 andYear:2015];
NSDate *endDate = [self getDateForDay:15 andMonth:8 andYear:2016];
现在,您需要从照片库中获取介于此范围之间的FetchResults。为此使用此方法:
-(PHFetchResult*) getAssetsFromLibraryWithStartDate:(NSDate *)startDate andEndDate:(NSDate*) endDate
{
PHFetchOptions *fetchOptions = [[PHFetchOptions alloc] init];
fetchOptions.predicate = [NSPredicate predicateWithFormat:@"creationDate > %@ AND creationDate < %@",startDate ,endDate];
PHFetchResult *allPhotos = [PHAsset fetchAssetsWithMediaType:PHAssetMediaTypeImage options:fetchOptions];
return allPhotos;
}
现在,您将获得PHFetchResults
该日期范围内存在的所有照片。现在,要提取本地标识符的数据数组,可以使用以下方法:
-(NSMutableArray *) getAssetIdentifiersForFetchResults:(PHFetchResult *) result{
NSMutableArray *identifierArray = [[NSMutableArray alloc] init];
for(PHAsset *asset in result){
NSString *identifierString = asset.localIdentifier;
[identifierArray addObject:identifierString];
}
return identifierArray;
}
现在,您将需要PHAsset
该图像。您可以像这样使用LocalIdentifier来获取PHAsset
:
-(void) getPHAssetWithIdentifier:(NSString *) localIdentifier andSuccessBlock:(void (^)(id asset))successBlock failure:(void (^)(NSError *))failureBlock{
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
NSArray *identifiers = [[NSArray alloc] initWithObjects:localIdentifier, nil];
PHFetchResult *savedAssets = [PHAsset fetchAssetsWithLocalIdentifiers:identifiers options:nil];
if(savedAssets.count>0)
{
successBlock(savedAssets[0]);
}
else
{
NSError *error;
failureBlock(error);
}
});
}
然后使用PHAsset
,您可以获取所需大小的图像(尝试将其保持为最小,以最大程度地减少内存使用量):
-(void) getImageForAsset: (PHAsset *) asset andTargetSize: (CGSize) targetSize andSuccessBlock:(void (^)(UIImage * photoObj))successBlock {
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
PHImageRequestOptions *requestOptions;
requestOptions = [[PHImageRequestOptions alloc] init];
requestOptions.resizeMode = PHImageRequestOptionsResizeModeFast;
requestOptions.deliveryMode = PHImageRequestOptionsDeliveryModeFastFormat;
requestOptions.synchronous = true;
PHImageManager *manager = [PHImageManager defaultManager];
[manager requestImageForAsset:asset
targetSize:targetSize
contentMode:PHImageContentModeDefault
options:requestOptions
resultHandler:^void(UIImage *image, NSDictionary *info) {
@autoreleasepool {
if(image!=nil){
successBlock(image);
}
}
}];
});
}
但是,请勿直接调用这些方法来获取所需的所有图像。
而是在您的cellForItemAtIndexPath
方法中调用这些方法,例如:
//Show spinner
[self getPHAssetWithIdentifier:yourLocalIdentifierAtIndexPath andSuccessBlock:^(id assetObj) {
PHAsset *asset = (PHAsset*)assetObj;
[self getImageForAsset:asset andTargetSize:yourTargetCGSize andSuccessBlock:^(UIImage *photoObj) {
dispatch_async(dispatch_get_main_queue(), ^{
//Update UI of cell
//Hide spinner
cell.imgViewBg.image = photoObj;
});
}];
} failure:^(NSError *err) {
//Some error occurred in fetching the image
}];
因此,结论是:
如果您想将所有资产汇总在一起,则可以使用fetchAssetCollectionWithLocalIdentifiers:方法来获取它,尽管我建议您反对使用它。
如果您有任何疑问或有任何其他反馈,请发表评论。
感谢Lyndsey Scott将PHFetchResult谓词设置为谓词,以便在此处的答案中获取两个日期之间的图像
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句