gpt4 book ai didi

iphone - 在 iPhone 中显示从 ALAsset 检索到的 URL 中的图像

转载 作者:行者123 更新时间:2023-12-03 18:09:22 24 4
gpt4 key购买 nike

我正在使用 ALAsset 框架来访问设备照片库中的文件。

到目前为止,我可以访问缩略图并显示它。
我想在 ImageView 中显示实际图像,但我不知道如何做到这一点。

我尝试使用 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;
}

最佳答案

API 稍微更改了规则,您不再能够通过文件系统直接访问 iPhoto 库。相反,您会得到这样的资源库 URL。

assets-library://asset/asset.JPG?id=1000000003&ext=JPG

您使用 ALAssetLibrary 对象通过 URL 访问 ALAsset 对象。

因此,从 ALAssetLibrary 的文档中,将其放入 header (或您的源代码)中

typedef void (^ALAssetsLibraryAssetForURLResultBlock)(ALAsset *asset);
typedef void (^ALAssetsLibraryAccessFailureBlock)(NSError *error);

这不是严格需要的,但可以让事情变得漂亮。
然后在你的源代码中。

-(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];
}
}

需要注意的几件事是,这使用了在我开始 iOS4 移植之前对我来说是新的 block ,但您可能想看看

https://www.mikeash.com/pyblog/friday-qa-2008-12-26.html

https://developer.apple.com/library/content/documentation/Cocoa/Conceptual/Blocks/Articles/00_Introduction.html

它们会让你稍微低下头,但如果你将它们视为通知选择器或回调,那就有点帮助了。

还有

  • findLargeImage 返回resultblock 还不会运行,因为它的回调。那么大的图片就不会了还有效。
  • largeImage 需要是实例变量的作用域不限于方法。

我在使用该方法时使用此构造来执行此操作,但您可能会找到更适合您使用的东西。

[node.view findLargeImage];
UIImage *thumb = node.view.largeImage;
if (thumb) { blah blah }

这就是我在尝试使其正常工作时学到的东西。

iOS 5 更新

当 iOS5 和单核设备上结果 block 触发时似乎有点慢,因此我不能依赖在调用 findLargeImage 后直接使用图像。所以我将其更改为调用代表。

@protocol HiresImageDelegate <NSObject>
@optional
-(void)hiresImageAvailable:(UIImage *)aimage;
@end

然后comme cá

//
ALAssetsLibraryAssetForURLResultBlock resultblock = ^(ALAsset *myasset)
{
ALAssetRepresentation *rep = [myasset defaultRepresentation];
CGImageRef iref = [rep fullResolutionImage];
if (iref) {
UIImage *largeimage = [UIImage imageWithCGImage:iref];
[delegate hiresImageAvailable:large];
}
};

关于iphone - 在 iPhone 中显示从 ALAsset 检索到的 URL 中的图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3837115/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com