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

发布于 2024-10-19 13:43:57 字数 450 浏览 4 评论 0原文

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

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

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

In my view controller, how can I know when a certain UIImageView has finished loading (large jpeg from documents directory)? I need to know so that I can then swap a placeholder low-res imageview with this hi-res imageview. Do I need to create a custom callback to know this? Any way is fine.

By the way, here is a snippet of code where I load the image:

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

软甜啾 2024-10-26 13:43:57

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: 。解决这些问题将作为读者的练习(或其他问题)。

UIImageView isn't doing any loading at all. All the loading is being done by [[UIImage alloc] initWithContentsOfFile:filePath], and your thread is blocked while the file is loaded (so the load is already complete by the time that call finally returns).

What you want to do is something like this:

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

You would set up self.imageView to display the low-res image and then call loadImage: to load the high-res version.

Note that if you call this repeatedly before didLoadImageInBackground: gets called from earlier calls, you may cause the device to run out of memory. Or you might have the image from the first call take so much longer to load than image from the second call that didLoadImageInBackground: gets called for the second image before it gets called for the first. Fixing those issues is left as an exercise for the reader (or for another question).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文