有关Web服务图像的问题无法在输出中显示

时间:2010-12-08 07:01:23

标签: iphone

NSURL *url=[NSURL URLWithString:elementurl];
NSLog(@"url %@", url);
NSData *data = [NSData dataWithContentsOfURL:url];
NSLog(@"data = %@, length = %i", data, [data length]);
UIImage *image = [[UIImage alloc] initWithData:data];
[data release];

iam not getting the data can any one suggest another method

2 个答案:

答案 0 :(得分:0)

pragma mark --------------------------

pragma mark加载UserImage

- (无效)loadUserImage {

NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];

NSString * frameImagePath = [profileInfoDict objectForKey:@“photo”];

imageData = [NSData dataWithContentsOfURL:[NSURL URLWithString:frameImagePath]];  if([imageData length] == 0){   / * NSString * imagePath = [[NSString alloc] initWithFormat:@“%@ /%@”,[[NSBundle mainBundle] resourcePath],@“no_image.png”];    UIImage image = [[UIImage alloc] initWithContentsOfFile:imagePath];    [item setObject:image forKey:@“itemImage”];    [imagePath发布];    [image release]; /  }  其他{   [self performSelectorOnMainThread:@selector(imageget :) withObject:imageData waitUntilDone:NO];

}  [池发布];

} - (void)imageget:(NSData *)数据 {  UIImage * image = [[UIImage alloc] initWithData:data];  userImageImageView.contentMode = UIViewContentModeScaleAspectFit;  userImage =图像;  userImageImageView.image = image;  [[mAppDelegate.userInfoArray objectAtIndex:1] setValue:image forKey:@“Image”];

}

答案 1 :(得分:0)

您使用的方法适用于小图像。如果您尝试下载大图像,则无法正常运行。

您可以使用以下代码

NSURLRequest * request = [NSURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:60];     connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];

建立连接

  • (void)connection:(NSURLConnection *)theConnection didReceiveData:(NSData *)incrementalData
  • (无效)connectionDidFinishLoading:(NSURLConnection的*)theConnection
  • (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error

以上方法用于接收数据,错误处理等......

现在使用此方法应用您自己的逻辑。