我正在使用ALAsset Framework访问设备照片库中的文件.
到目前为止,我可以访问缩略图并显示它.
我想在图像视图中显示实际图像,但我无法弄清楚如何执行此操作.
我尝试使用ALAsset对象中的URL字段但未成功.
谁知道如何做到这一点?
这是我能够访问缩略图并将其放在表格单元格中的一些代码:
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
//here 'asset' represents the ALAsset object
asset = [assets objectAtIndex:indexPath.row];
//i am accessing the thumbnail here
[cell.imageView setImage:[UIImage imageWithCGImage:[asset thumbnail]]];
[cell.textLabel setText:[NSString stringWithFormat:@"Photo %d", indexPath.row+1]];
return cell;
}
Run Code Online (Sandbox Code Playgroud)
War*_*ton 93
API已稍微更改了规则,您无法再直接访问iPhoto图库.相反,你得到这样的资产库URL.
assets-library://asset/asset.JPG?id=1000000003&ext=JPG
您使用ALAssetLibrary对象通过URL访问ALAsset对象.
所以从ALAssetLibrary的文档中将其抛入标题(或您的源代码)中
typedef void (^ALAssetsLibraryAssetForURLResultBlock)(ALAsset *asset);
typedef void (^ALAssetsLibraryAccessFailureBlock)(NSError *error);
Run Code Online (Sandbox Code Playgroud)
这不是严格需要的,但保持美观.
然后在你的来源.
-(void)findLargeImage
{
NSString *mediaurl = [self.node valueForKey:kVMMediaURL];
//
ALAssetsLibraryAssetForURLResultBlock resultblock = ^(ALAsset *myasset)
{
ALAssetRepresentation *rep = [myasset defaultRepresentation];
CGImageRef iref = [rep fullResolutionImage];
if (iref) {
largeimage = [UIImage imageWithCGImage:iref];
[largeimage retain];
}
};
//
ALAssetsLibraryAccessFailureBlock failureblock = ^(NSError *myerror)
{
NSLog(@"booya, cant get image - %@",[myerror localizedDescription]);
};
if(mediaurl && [mediaurl length] && ![[mediaurl pathExtension] isEqualToString:AUDIO_EXTENSION])
{
[largeimage release];
NSURL *asseturl = [NSURL URLWithString:mediaurl];
ALAssetsLibrary* assetslibrary = [[[ALAssetsLibrary alloc] init] autorelease];
[assetslibrary assetForURL:asseturl
resultBlock:resultblock
failureBlock:failureblock];
}
}
Run Code Online (Sandbox Code Playgroud)
需要注意的一点是,在我开始iOS4移植之前,这会使用对我来说不熟悉的块,但是你可能想看一下
https://www.mikeash.com/pyblog/friday-qa-2008-12-26.html
和
他们弯曲了一下你的脑袋,但如果你认为它们是通知选择器或回调,它会有所帮助.
也
findLargeImage返回时,结果块不会作为回调运行.所以largeImage还没有效果.largeImage 需要是一个不限定方法的实例变量.我在使用该方法时使用此构造来执行此操作,但您可能会找到更适合您的方法.
[node.view findLargeImage];
UIImage *thumb = node.view.largeImage;
if (thumb) { blah blah }
Run Code Online (Sandbox Code Playgroud)
这就是我在试图让这个工作时学到的东西.
iOS 5更新
使用iOS5和单核设备时,结果块触发的速度似乎有点慢,所以我无法在调用后直接使用该图像findLargeImage.所以我把它改成了一个代表.
@protocol HiresImageDelegate <NSObject>
@optional
-(void)hiresImageAvailable:(UIImage *)aimage;
@end
Run Code Online (Sandbox Code Playgroud)
和commecá
//
ALAssetsLibraryAssetForURLResultBlock resultblock = ^(ALAsset *myasset)
{
ALAssetRepresentation *rep = [myasset defaultRepresentation];
CGImageRef iref = [rep fullResolutionImage];
if (iref) {
UIImage *largeimage = [UIImage imageWithCGImage:iref];
[delegate hiresImageAvailable:large];
}
};
Run Code Online (Sandbox Code Playgroud)
小智 15
沃伦的回答对我很有用.对于某些人来说,一个有用的事情是同时包括图像方向和比例元数据.您可以在结果块中执行此操作,如下所示:
ALAssetsLibraryAssetForURLResultBlock resultblock = ^(ALAsset *myasset)
{
ALAssetRepresentation *rep = [myasset defaultRepresentation];
CGImageRef iref = [rep fullResolutionImage];
if (iref)
{
UIImage *largeimage = [UIImage imageWithCGImage:iref scale:[rep scale] orientation:[rep orientation]];
[delegate hiresImageAvailable:large];
}
};
Run Code Online (Sandbox Code Playgroud)
imageWIthCGImage在这种情况下,调用具有比例,并orientation在UIImage为您创建时添加.
[UIImage imageWithCGImage:iref scale:[rep scale] orientation:[rep orientation]];
Run Code Online (Sandbox Code Playgroud)
需要注意的一个技巧是,如果你使用[rep fullScreenImage]而不是[rep fullResolutionImage]在iOS 5上,你会得到一个已经旋转过的图像 - 但是它的分辨率是iPhone屏幕 - 即它的分辨率较低.
只是将Warren和oknox的答案组合成一个较短的片段:
ALAssetsLibrary *assetsLibrary = [[ALAssetsLibrary alloc] init];
[assetsLibrary assetForURL:self.selectedPhotos[i] resultBlock: ^(ALAsset *asset){
ALAssetRepresentation *representation = [asset defaultRepresentation];
CGImageRef imageRef = [representation fullResolutionImage];
if (imageRef) {
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
imageView.image = [UIImage imageWithCGImage:imageRef scale:representation.scale orientation:representation.orientation];
// ...
}
} failureBlock: ^{
// Handle failure.
}];
Run Code Online (Sandbox Code Playgroud)
我个人喜欢设置我failureBlock的nil.
NSURL* aURL = [NSURL URLWithString:@"URL here"];
ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
[library assetForURL:aURL resultBlock:^(ALAsset *asset)
{
UIImage *copyOfOriginalImage = [UIImage imageWithCGImage:[[asset defaultRepresentation] fullScreenImage] scale:0.5 orientation:UIImageOrientationUp];
cell.backgroundView = [[UIImageView alloc] initWithImage:copyOfOriginalImage];
}
failureBlock:^(NSError *error)
{
// error handling
NSLog(@"failure-----");
}];
Run Code Online (Sandbox Code Playgroud)
只需提供你在上面提供的photolibrary图像中获得的UIReferenceURl ......它的工作正常..我把它浸透了
UIcollectionView单元格
..如果你只是想把它展示出来
的UIImageView
手段
更改
cell.backgroundView = [[UIImageView alloc] initWithImage:copyOfOriginalImage];
Run Code Online (Sandbox Code Playgroud)
至
imageView.image = copyOfOriginalImage;
Run Code Online (Sandbox Code Playgroud)