如何使用 ios7 中的 ALAssetLibrary 即时获取所有照片?
How to get all photos moment wise using ALAssetLibrary in ios7?
我无法像苹果 iphone 在 ios8 中那样即时获取照片。我已经使用 PHAsset 和 Photos.framework 实现了 ios8。现在,当我 运行 在 ios7 设备中使用相同的代码时,它 returns 什么都没有。所以,我使用 ALAssetLibrary 来获取照片。使用 ALAssetLibrary 我也得到了所有照片,但这些照片就像相册明智的照片。并且还使用这个 ALAssetLibrary 我无法获得相册创建日期而不是它的位置名称,因为我必须向每个部分的 header 上的数据显示它。
我使用 ALAssetLibrary 在 ios7 中获取照片的代码:
_imagearray = [@[] mutableCopy];
__block NSMutableArray *tmpAssets = [@[] mutableCopy];
ALAssetsLibrary *assetLibrary = [[ALAssetsLibrary alloc] init];
[assetLibrary enumerateGroupsWithTypes:ALAssetsGroupAll usingBlock:^(ALAssetsGroup *group, BOOL *stop)
{
if (group)
{
[group setAssetsFilter:[ALAssetsFilter allAssets]];
[group enumerateAssetsWithOptions:NSEnumerationReverse usingBlock:^(ALAsset *asset, NSUInteger index, BOOL *stop) {
if (asset!=nil)
{
[tmpAssets addObject:asset];
}
}];
}
self.imagearray = tmpAssets;
NSLog (@“%@“,self.imagearray);
}
failureBlock:^(NSError *error)
{
NSLog(@"error enumerating AssetLibrary groups %@\n", error);
}];
你运气不好 iOS 7. 你观察到的 AssetsLibrary returns 只有相册(相机胶卷,用户相册)。尽管 iOS 7 中的照片应用程序显示了 Moments,但 iOS 7 中没有开发者 API 来获取 Moments。
我自己找到了解决方案。
+ (ALAssetsLibrary *)defaultAssetsLibrary {
static dispatch_once_t pred = 0;
static ALAssetsLibrary *library = nil;
dispatch_once(&pred, ^{
library = [[ALAssetsLibrary alloc] init];
});
return library;
}
-(void)loadAssets{
NSMutableArray *unSortArray = [[NSMutableArray alloc] init];
ALAssetsLibrary *library = [MomentsVCTR defaultAssetsLibrary];
[library enumerateGroupsWithTypes:ALAssetsGroupAll usingBlock:^(ALAssetsGroup *group, BOOL *stop) {
if (group == nil) {
NSLog(@"Done!");
[self manageLocalAssets:unSortArray];
}
[group setAssetsFilter:[ALAssetsFilter allAssets]];
[group enumerateAssetsWithOptions:NSEnumerationReverse usingBlock:^(ALAsset *alAsset, NSUInteger index, BOOL *innerStop) {
if (alAsset) {
[unSortArray addObject:alAsset];
}
}];
} failureBlock: ^(NSError *error) {
NSLog(@"No groups: %@",error);
}];
}
-(void)manageLocalAssets:(NSMutableArray*)unSortArray{
NSMutableArray *_resultArray = [[NSMutableArray alloc] init];
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateFormat:@"dd-MMM-yyyy"];
NSLog(@"in loadassets");
NSSortDescriptor *descriptor=[[NSSortDescriptor alloc] initWithKey:@"date" ascending:NO];
NSArray *descriptors=[NSArray arrayWithObject: descriptor];
NSArray *reverseOrder=[unSortArray sortedArrayUsingDescriptors:descriptors];
for (int k=0; k<reverseOrder.count; k++) {
ALAsset *asset = (ALAsset *)[reverseOrder objectAtIndex:k];
NSString *dateStr = [df stringFromDate:[asset valueForProperty:ALAssetPropertyDate]];
if (![self.arrDate containsObject:dateStr]) {
[self.arrDate addObject:dateStr];
[self.arrEventID addObject:@"0"];
[self.arrEventName addObject:@"0"];
}
[_resultArray addObject:asset];
}
for (int i=0;i<self.arrDate.count;i++) {
NSMutableArray *arr = [[NSMutableArray alloc] init];
NSMutableArray *arr2 = [[NSMutableArray alloc] init];
int tPhoto = 0;
int tVideo = 0;
for (int j=0; j<_resultArray.count; j++) {
ALAsset *asset = (ALAsset*)[_resultArray objectAtIndex:j];
NSString *dateStr = [df stringFromDate:[asset valueForProperty:ALAssetPropertyDate]];
if ([[self.arrDate objectAtIndex:i] isEqualToString:dateStr]) {
UIImage *latestPhotoThumbnail = [UIImage imageWithCGImage:[asset thumbnail]];
[arr addObject:latestPhotoThumbnail];
latestPhotoThumbnail = nil;
if ([[asset valueForProperty:ALAssetPropertyType] isEqualToString:ALAssetTypeVideo]) {
[arr2 addObject:@"1"];
tVideo++;
}
else{
[arr2 addObject:@"0"];
tPhoto++;
}
NSDate *date = [asset valueForProperty:ALAssetPropertyDate];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setLocale:[NSLocale currentLocale]];
[dateFormatter setDateFormat:@"dd-MMM-yyyy"];
[self.imageDateArray addObject:[dateFormatter stringFromDate:date]];
[self.imageIdArray addObject:[NSString stringWithFormat:@"%d",i]];
}
}
[self.imagearray addObject:arr];
[self.arrContentType addObject:arr2];
[self.momentArray addObject:[NSString stringWithFormat:@"%lu",(unsigned long)arr.count]];
[self.arrPhotoCount addObject:[NSString stringWithFormat:@"%d",tPhoto]];
[self.arrVideoCount addObject:[NSString stringWithFormat:@"%d",tVideo]];
}
[self setButtonsSize];
self.collection.dataSource = self;
self.collection.delegate = self;
[self.collection reloadData];
[self.collection.collectionViewLayout invalidateLayout];
self.footerView.hidden = TRUE;
self.footerWebView.hidden = TRUE;
}
我无法像苹果 iphone 在 ios8 中那样即时获取照片。我已经使用 PHAsset 和 Photos.framework 实现了 ios8。现在,当我 运行 在 ios7 设备中使用相同的代码时,它 returns 什么都没有。所以,我使用 ALAssetLibrary 来获取照片。使用 ALAssetLibrary 我也得到了所有照片,但这些照片就像相册明智的照片。并且还使用这个 ALAssetLibrary 我无法获得相册创建日期而不是它的位置名称,因为我必须向每个部分的 header 上的数据显示它。
我使用 ALAssetLibrary 在 ios7 中获取照片的代码:
_imagearray = [@[] mutableCopy];
__block NSMutableArray *tmpAssets = [@[] mutableCopy];
ALAssetsLibrary *assetLibrary = [[ALAssetsLibrary alloc] init];
[assetLibrary enumerateGroupsWithTypes:ALAssetsGroupAll usingBlock:^(ALAssetsGroup *group, BOOL *stop)
{
if (group)
{
[group setAssetsFilter:[ALAssetsFilter allAssets]];
[group enumerateAssetsWithOptions:NSEnumerationReverse usingBlock:^(ALAsset *asset, NSUInteger index, BOOL *stop) {
if (asset!=nil)
{
[tmpAssets addObject:asset];
}
}];
}
self.imagearray = tmpAssets;
NSLog (@“%@“,self.imagearray);
}
failureBlock:^(NSError *error)
{
NSLog(@"error enumerating AssetLibrary groups %@\n", error);
}];
你运气不好 iOS 7. 你观察到的 AssetsLibrary returns 只有相册(相机胶卷,用户相册)。尽管 iOS 7 中的照片应用程序显示了 Moments,但 iOS 7 中没有开发者 API 来获取 Moments。
我自己找到了解决方案。
+ (ALAssetsLibrary *)defaultAssetsLibrary {
static dispatch_once_t pred = 0;
static ALAssetsLibrary *library = nil;
dispatch_once(&pred, ^{
library = [[ALAssetsLibrary alloc] init];
});
return library;
}
-(void)loadAssets{
NSMutableArray *unSortArray = [[NSMutableArray alloc] init];
ALAssetsLibrary *library = [MomentsVCTR defaultAssetsLibrary];
[library enumerateGroupsWithTypes:ALAssetsGroupAll usingBlock:^(ALAssetsGroup *group, BOOL *stop) {
if (group == nil) {
NSLog(@"Done!");
[self manageLocalAssets:unSortArray];
}
[group setAssetsFilter:[ALAssetsFilter allAssets]];
[group enumerateAssetsWithOptions:NSEnumerationReverse usingBlock:^(ALAsset *alAsset, NSUInteger index, BOOL *innerStop) {
if (alAsset) {
[unSortArray addObject:alAsset];
}
}];
} failureBlock: ^(NSError *error) {
NSLog(@"No groups: %@",error);
}];
}
-(void)manageLocalAssets:(NSMutableArray*)unSortArray{
NSMutableArray *_resultArray = [[NSMutableArray alloc] init];
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateFormat:@"dd-MMM-yyyy"];
NSLog(@"in loadassets");
NSSortDescriptor *descriptor=[[NSSortDescriptor alloc] initWithKey:@"date" ascending:NO];
NSArray *descriptors=[NSArray arrayWithObject: descriptor];
NSArray *reverseOrder=[unSortArray sortedArrayUsingDescriptors:descriptors];
for (int k=0; k<reverseOrder.count; k++) {
ALAsset *asset = (ALAsset *)[reverseOrder objectAtIndex:k];
NSString *dateStr = [df stringFromDate:[asset valueForProperty:ALAssetPropertyDate]];
if (![self.arrDate containsObject:dateStr]) {
[self.arrDate addObject:dateStr];
[self.arrEventID addObject:@"0"];
[self.arrEventName addObject:@"0"];
}
[_resultArray addObject:asset];
}
for (int i=0;i<self.arrDate.count;i++) {
NSMutableArray *arr = [[NSMutableArray alloc] init];
NSMutableArray *arr2 = [[NSMutableArray alloc] init];
int tPhoto = 0;
int tVideo = 0;
for (int j=0; j<_resultArray.count; j++) {
ALAsset *asset = (ALAsset*)[_resultArray objectAtIndex:j];
NSString *dateStr = [df stringFromDate:[asset valueForProperty:ALAssetPropertyDate]];
if ([[self.arrDate objectAtIndex:i] isEqualToString:dateStr]) {
UIImage *latestPhotoThumbnail = [UIImage imageWithCGImage:[asset thumbnail]];
[arr addObject:latestPhotoThumbnail];
latestPhotoThumbnail = nil;
if ([[asset valueForProperty:ALAssetPropertyType] isEqualToString:ALAssetTypeVideo]) {
[arr2 addObject:@"1"];
tVideo++;
}
else{
[arr2 addObject:@"0"];
tPhoto++;
}
NSDate *date = [asset valueForProperty:ALAssetPropertyDate];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setLocale:[NSLocale currentLocale]];
[dateFormatter setDateFormat:@"dd-MMM-yyyy"];
[self.imageDateArray addObject:[dateFormatter stringFromDate:date]];
[self.imageIdArray addObject:[NSString stringWithFormat:@"%d",i]];
}
}
[self.imagearray addObject:arr];
[self.arrContentType addObject:arr2];
[self.momentArray addObject:[NSString stringWithFormat:@"%lu",(unsigned long)arr.count]];
[self.arrPhotoCount addObject:[NSString stringWithFormat:@"%d",tPhoto]];
[self.arrVideoCount addObject:[NSString stringWithFormat:@"%d",tVideo]];
}
[self setButtonsSize];
self.collection.dataSource = self;
self.collection.delegate = self;
[self.collection reloadData];
[self.collection.collectionViewLayout invalidateLayout];
self.footerView.hidden = TRUE;
self.footerWebView.hidden = TRUE;
}