从网站的URL创建UIImage

时间:2015-09-28 11:48:23

标签: ios objective-c uiimage nsdata nsurl

NSURL *imgURL = [NSURL URLWithString:@"stackoverflow.com"];
NSData *imgData = [NSData dataWithContentsOfURL:imgURL];
UIImage *img = [[UIImage alloc] initWithData:imgData];

使用这些代码,是否可以从网站的URL中创建UIImage?很抱歉,如果问题不明确,我的意思是截图整个网页以生成UIImage。我可以用这些吗?

1 个答案:

答案 0 :(得分:0)

从这个链接。是的你可以。

NSURL *imgURL = [NSURL URLWithString:@"http://www.gettyimages.in/gi-resources/images/Homepage/Hero/UK/CMS_Creative_164657191_Kingfisher.jpg
"];
NSData *imgData = [NSData dataWithContentsOfURL:imgURL];
UIImage *img = [[UIImage alloc] initWithData:imgData];

如果您需要在不冻结应用程序的情况下加载图像,还要实现缩略图和缓存,

尝试SDWebImage框架

https://github.com/rs/SDWebImage

示例代码

[imageView sd_setImageWithURL:[NSURL URLWithString:@"Get each url from array and set it here using indexpath.row"]
                  placeholderImage:[UIImage imageNamed:@"placeholder.png"]];

<强>截图

UIGraphicsBeginImageContextWithOptions(self.view.bounds.size, self.view.opaque, 0.0);
[self.myView.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *imageView = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

NSData *imageData = UIImageJPEGRepresentation(imageView, 1.0 ); //you can use PNG too