
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 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:创建一个原始数据用零填充的数据对象,用init只替换initWithLength:它会起作用.
总结以上是内存溢出为你收集整理的ios – 是否存在UIImage完成加载的事件全部内容,希望文章能够帮你解决ios – 是否存在UIImage完成加载的事件所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)