gpt4 book ai didi

iphone - 如何知道 UIimageView 何时完成加载?

转载 作者:行者123 更新时间:2023-12-03 20:56:09 25 4
gpt4 key购买 nike

在我的 View Controller 中,我如何知道某个 UIImageView 何时完成加载(来自文档目录的大 jpeg)?我需要知道,以便我可以将占位符低分辨率 ImageView 与此高分辨率 ImageView 交换。我是否需要创建自定义回调才能知道这一点?任何方式都可以。

顺便说一下,这是我加载图像的代码片段:

NSString *fileName = [NSString stringWithFormat:@"hires_%i.jpg", currentPage];
NSString *filePath = [NSString stringWithFormat:@"%@/BookImage/%@", [self documentsDirectory], fileName];
hiResImageView.image = [[[UIImage alloc] initWithContentsOfFile:filePath] autorelease];

最佳答案

UIImageView 根本不进行任何加载。所有加载都是由 [[UIImage alloc] initWithContentsOfFile:filePath] 完成的,并且在加载文件时您的线程被阻塞(因此在调用最终返回时加载已经完成)。

你想做的是这样的:

- (void)loadImage:(NSString *)filePath {
[self performSelectorInBackground:@selector(loadImageInBackground:) withObject:filePath];
}

- (void)loadImageInBackground:(NSString *)filePath {
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
UIImage *image = [[UIImage alloc] initWithContentsOfFile:filePath];
[self performSelectorOnMainThread:@selector(didLoadImageInBackground:) withObject:image waitUntilDone:YES];
[image release];
[pool release];
}

- (void)didLoadImageInBackground:(UIImage *)image {
self.imageView.image = image;
}

您可以设置self.imageView来显示低分辨率图像,然后调用loadImage:来加载高分辨率版本。

请注意,如果您在之前的调用中调用 didLoadImageInBackground: 之前重复调用此函数,则可能会导致设备内存不足。或者,您可能会发现第一次调用中的图像比第二次调用中的图像需要更长的时间来加载,以至于在调用第一个图像之前先调用第二个图像的 didLoadImageInBackground: 。解决这些问题将作为读者的练习(或其他问题)。

关于iphone - 如何知道 UIimageView 何时完成加载?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5132958/

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