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

本文介绍了一种使用Objective-C从网络URL加载图片的方法,并将其显示在UIImageView中。通过将图片数据转换为UIImage并设置到UIImageView,实现了图片的正确显示。此外,还提供了解决URL可能存在的特殊字符导致加载失败的问题。
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]];

转载于:https://www.cnblogs.com/top5/archive/2012/03/23/2414336.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值