(iphone开发)从网络读取图片并显示

NSString *urlString = @"http://img4.bitauto.com/autoalbum/files/20100226/003/201002260944098032_1155951_4.jpg";
NSURL *url = [NSURL URLWithString:urlString];
NSData *myData = [NSData dataWithContentsOfURL: url];
UIImage *myImage = [UIImage imageWithData: myData];
UIImageView *myImageView = [[UIImageView alloc] initWithImage: myImage];
UIScrollView *myScrollView = [[UIScrollView alloc] initWithFrame: CGRectMake(0, 0, 320, 480)];
myScrollView.contentSize = CGSizeMake(myImage.size.width, myImage.size.height;
[self.view addSubview: myScrollView];
[myScrollView release];
[myScrollView addSubview: myImageView];
[myImageView release];
如果返回url是nil,则用下面的代码代替:
NSURL *url = [NSURL URLWithString:[urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
posted @ 2012-03-23 22:04  与时俱进  阅读(567)  评论(0编辑  收藏  举报
友情链接:同里老宅院民居客栈