HTML5   发布时间:2022-04-27  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了ios – 是否存在UIImage完成加载的事件大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在使用以下代码显示UI Image视图:

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何时完成显示图像?

非常感谢.

解决方法

你的.h

@interface YourClass : YourSuperclass<NSURLConnectionDataDelegate>

@property (nonatomiC) NSMutableData *imageData;
@property (nonatomiC) NSUInteger @R_552_10586@lBytes;
@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.@R_552_10586@lBytes = length.unsignedIntegerValue;

    [self.imageData = [[NSMutableData alloc] initWithLength:self.@R_552_10586@lBytes];
}

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

    // Actual progress is self.receivedBytes / self.@R_552_10586@lBytes
}

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

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

更新:

由于NSMutableData initWithLength:创建一个原始数据用零填充的数据对象,用init只替换initWithLength:它会起作用.

大佬总结

以上是大佬教程为你收集整理的ios – 是否存在UIImage完成加载的事件全部内容,希望文章能够帮你解决ios – 是否存在UIImage完成加载的事件所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。