UIImage加载完成后是否有事件?

5

我正在使用以下代码显示UIImage视图:

NSURL *imageUrl = [[NSURL alloc]initWithString:@"http://..."];
NSData *imageData = [[NSData alloc] initWithContentsOfURL:imageUrl];
UIImage *audioArt =[[UIImage alloc] initWithData:imageData];
UIImageView *artView =[[UIImageView alloc] initWithImage:audioArt];

artView.contentMode = UIViewContentModeScaleAspectFit;
artView.autoresizesSubviews = NO;
artView.frame = viewRef.bounds;
[artView setAutoresizingMask:UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight];
[artView setBackgroundColor:[UIColor blackColor]];

[viewRef setBackgroundColor:[UIColor blackColor]];
[viewRef addSubview:artView];

有没有一个Objective C事件可以告诉我们这个UIImage何时加载完成或UIImageView何时显示完图片?非常感谢。
2个回答

4
在你的 .h
@interface YourClass : YourSuperclass<NSURLConnectionDataDelegate>

@property (nonatomic) NSMutableData *imageData;
@property (nonatomic) NSUInteger totalBytes;
@property (nonatomic) NSUInteger receivedBytes;

同时调用某个位置

NSURL *imageUrl = [[NSURL alloc]initWithString:@"http://..."];
NSURLRequest *request = [NSURLRequest requestWithURL: imageUrl];
NSURLConnection *connection = [NSURLConnection alloc] initWithRequest:request delegate:self startImmediately:YES];

同时还需要实现委托方法。
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
    NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *) urlResponse;
    NSDictionary *dict = httpResponse.allHeaderFields;
    NSString *lengthString = [dict valueForKey:@"Content-Length"];
    NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
    NSNumber *length = [formatter numberFromString:lengthString];
    self.totalBytes = length.unsignedIntegerValue;

    [self.imageData = [[NSMutableData alloc] initWithLength:self.totalBytes];
}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
    [self.imageData appendData:data];
    self.receivedBytes += data.length;

    // Actual progress is self.receivedBytes / self.totalBytes
}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
    imageView.image = [UIImage imageWithData:self.imageData];
}

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
    //handle error
}

更新:

由于NSMutableData initWithLength:创建的数据对象的原始数据已填充为零,因此请将initWithLength:替换为init即可。


谢谢您。我已经记录了整个过程,而且我确实收到了数据。但是最后imageView.image=(null) - 您能想到任何原因吗? - Jimmery
你已经分配好了imageView吗? - Mutawe
是的,图像视图已经被分配了。当连接完成加载时,我使用NSLog打印self.imageData,数据以大量零(约几KB的零)开头 - 我尝试过类似于if(data!=nil){[self.imageData appendData:data]}的操作,但这并没有起作用。你有什么想法吗? - Jimmery
1
NSMutableData initWithLength: 创建一个数据对象,其原始数据填充为零。 - Mutawe
1
啊,我用init替换了initWithLength:,现在它非常好用!非常感谢! - Jimmery
欢迎,很高兴能帮助您。 - Mutawe

1

UIImage 没有任何代理或回调来告诉你它是否成功加载。

如果您从任何 URL 获取图像,则可以使用 NSURL 的代理和通知来跟踪是否已接收到图像。

另外,您也可以通过以下方式实现:

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

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

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

1
OP 对于“加载”一词的理解有些不清楚。但是这个“通知”不会在图像“加载”时被调度。这里的“加载”指的是当图像加载其数据、编码数据并最终由 UIKit 渲染时。UIImage lazily “加载”图像。initWithContentsOfFile: 可能只会在内部设置文件路径,仅此而已。 - CouchDeveloper

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接