获取iphone相册方法:
方法一:
ALAssetsLibraryGroupsEnumerationResultsBlock listGroupBlock =
^(ALAssetsGroup *group, BOOL *stop) {
if (group!=nil) {
[groups addObject:group];
} else {
[self.tableView performSelectorOnMainThread:@selector(reloadData)
withObject:nil waitUntilDone:YES];
}
};
ALAssetsLibraryAccessFailureBlock failureBlock = ^(NSError *error)
{
NSString *errorMessage = nil;
switch ([error code]) {
case ALAssetsLibraryAccessUserDeniedError:
case ALAssetsLibraryAccessGloballyDeniedError:
errorMessage = @"The user has declined access to it.";
break;
default:
errorMessage = @"Reason unknown.";
break;
}
UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"Opps"
message:errorMessage delegate:self cancelButtonTitle:@"Cancel"
otherButtonTitles:nil, nil];
[alertView show];
[alertView release];
};
NSUInteger groupTypes = ALAssetsGroupAlbum | ALAssetsGroupEvent |
ALAssetsGroupFaces;
ALAssetsLibrary *assetsLibrary = [[ALAssetsLibrary alloc] init];
[assetsLibrary enumerateGroupsWithTypes:groupTypes
usingBlock:listGroupBlock failureBlock:failureBlock];
[assetsLibrary release];
方法二:
void (^assetGroupEnumerator)(ALAssetsGroup *, BOOL *) =
^(ALAssetsGroup *group, BOOL *stop)
{
if (group == nil)
{
[self.tableView performSelectorOnMainThread:@selector(reloadData)
withObject:nil waitUntilDone:YES];
return;
}
[groups addObject:group];
};
void (^assetGroupEnumberatorFailure)(NSError *) = ^(NSError *error)
{
NSString *errorMessage = nil;
switch ([error code]) {
case ALAssetsLibraryAccessUserDeniedError:
case ALAssetsLibraryAccessGloballyDeniedError:
errorMessage = @"The user has declined access to it.";
break;
default:
errorMessage = @"Reason unknown.";
break;
}
UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"Opps"
message:errorMessage delegate:self cancelButtonTitle:@"Cancel"
otherButtonTitles:nil, nil];
[alertView show];
[alertView release];
};
ALAssetsLibrary *assetsLibrary = [[ALAssetsLibrary alloc] init];
[assetsLibrary enumerateGroupsWithTypes:ALAssetsGroupAll
usingBlock:assetGroupEnumerator
failureBlock:assetGroupEnumberatorFailure];
[assetsLibrary release];
以上是获取所有ALAssetsGroup,即相册对象。
获取相册的标示照片:
ALAssetsGroup *group = [groups objectAtIndex:index];
CGImageRef posterImageRef = [group posterImage];
UIImage *posterImage = [UIImage
imageWithCGImage:posterImageRef];
获取相册中的所有对象(相片和录像等):
group = (ALAssetsGroup*)[assetGroups objectAtIndex:index];
[group setAssetsFilter:[ALAssetsFilter allAssets]];
[group enumerateAssetsUsingBlock:^(ALAsset *result, NSUInteger
index, BOOL *stop)
{
if(result == nil)
{
return;
}
//获取相片的url:
NSString *url = [[[result
defaultRepresentation]url]description];
[self.urls addObject:url];
[self.assets addObject:result];
}];
只获取照片的方法:
ALAssetsGroupEnumerationResultsBlock assetsEnumerationBlock =
^(ALAsset *result, NSUInteger index, BOOL *stop) {
if (result) {
[assets addObject:result];
}
};
ALAssetsFilter *onlyPhotosFilter = [ALAssetsFilter allPhotos];
[assetsGroup setAssetsFilter:onlyPhotosFilter];
[assetsGroup enumerateAssetsUsingBlock:assetsEnumerationBlock];
获得的ALAsset对象就是相片对象:其中有相片的缩略图,全屏图,高清图,url等属性。
ALAsset *result = [assets objectAtIndex:index];
获取url:
String类型:
NSString *url = [[[result
defaultRepresentation]url]description];
URL类型:
NSURL *url = [[result defaultRepresentation]url];
获取缩略图:
CGImageRef ref = [result thumbnail];
UIImage *img = [[UIImage alloc]initWithCGImage:ref];
获取全屏相片:
CGImageRef ref = [[result defaultRepresentation]fullScreenImage];
UIImage *img = [[UIImage alloc]initWithCGImage:ref];
获取高清相片:
CGImageRef ref = [[result defaultRepresentation]fullResolutionImage];
UIImage *img = [[UIImage alloc]initWithCGImage:ref];
根据ALAsset的url获取ALAsset对象:
void (^assetRseult)(ALAsset *) = ^(ALAsset *result)
{
if (result == nil)
{
return;
}
UIImage *image = [UIImage imageWithCGImage:[[result defaultRepresentation]fullScreenImage]];
[self.images addObject:image];
[self.assetArrays addObject:result];
};
void (^failureBlock)(NSError *) = ^(NSError *error) {
UIAlertView * alert = [[UIAlertView alloc]
initWithTitle:@"Error"
message:[NSString stringWithFormat:@"Error: %@", [error description]]
delegate:nil
cancelButtonTitle:@"Ok"
otherButtonTitles:nil];
[alert show];
[alert release];
NSLog(@"A problem occured %@", [error description]);
};
ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
[library assetForURL:assetUrl resultBlock:assetRseult failureBlock:failureBlock];
[library release];
resultBlock也可以如此定义:
ALAssetsLibraryAssetForURLResultBlock assetsResultBlock = ^(ALAsset *result)
{
if (result == nil)
{
return;
}
UIImage *image = [UIImage imageWithCGImage:[[result defaultRepresentation]fullScreenImage]];
[self.images addObject:image];
[self.assetArrays addObject:result];
};
获取照片的时间:
-(void)getDate:(ALAsset*)rule
{
NSDictionary *dic = [[rule defaultRepresentation]metadata];
id dateTime = [[dic objectForKey:@"{TIFF}"]objectForKey:@"DateTime"];
if (dateTime!=nil) {
NSArray *time = [dateTime
componentsSeparatedByCharactersInSet:[NSCharacterSet
whitespaceCharacterSet]];
NSString *dataStr = [time objectAtIndex:0];
if (![date containsObject:dataStr]) {
[date addObject:[time objectAtIndex:0]];
}
}
}