Ios UIImage完成加载时是否存在事件
我正在使用以下代码显示UIImage视图:Ios UIImage完成加载时是否存在事件,ios,objective-c,uiimage,Ios,Objective C,Uiimage,我正在使用以下代码显示UIImage视图: NSURL *imageUrl = [[NSURL alloc]initWithString:@"http://..."]; NSData *imageData = [[NSData alloc] initWithContentsOfURL:imageUrl]; UIImage *audioArt =[[UIImage alloc] initWithData:imageData]; UIImageView *artView =[[UIImageView
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何时完成图像显示
非常感谢。
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;
}
在您的
.h
@interface YourClass : YourSuperclass<NSURLConnectionDataDelegate>
@property (nonatomic) NSMutableData *imageData;
@property (nonatomic) NSUInteger totalBytes;
@property (nonatomic) NSUInteger receivedBytes;
并实现了委托方法
- (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:
替换为initWithLength:
,它就会工作。OP对“加载”的理解有点不清楚。但此“通知”不会安排在图像“加载”的时间点。其中“已加载”是指图像已加载其数据、对数据进行编码,并且UIKit已最终呈现该数据。UIImage惰性地“加载”图像initWithContentsOfFile:
可能只在内部设置文件路径,就是这样。谢谢。我已经记录了整个过程,我肯定收到了数据。然而,在这一切的最后,imageView.image=(null)
-你能想出任何原因吗?你分配了你的imageView吗?是的,图像视图被分配了,当I NSLogself.imageData
当连接完成加载时,数据以零的加载开始(比如几kb的零)-我试过做一些类似于if(data!=nil){[self.imageData appendData:data]}
的事情,但这不起作用。你有什么想法吗?NSMutableData initWithLength:
创建了一个数据对象,它的原始数据填充了zerosah,我用initWithLength:
替换了initWithLength:
,效果非常好!非常感谢!
- (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
}