2015-09-28 82 views
1
NSURL *imgURL = [NSURL URLWithString:@"stackoverflow.com"]; 
NSData *imgData = [NSData dataWithContentsOfURL:imgURL]; 
UIImage *img = [[UIImage alloc] initWithData:imgData]; 

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

+0

你的意思是网站的 “截图”?不,你不能做到这一点,使用WKWebView来呈现网站,然后你可以从该呈现图像。 – Igor

+0

你不能从url创建图像,是的,但你可以从url加载图像,如果你有图像的网址(不是网站)。 –

+0

@Igor是的,我正在使用WKWebView,我尝试使用'renderInContext:'和'drawViewHierarchyInRect:afterScreenUpdates:'渲染它,但我没有得到我想要的图像。 –

回答

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 
+0

我认为所以他要求制作URL的缩略图。不下载图片 – Mukesh

+0

提问者要求创建拇指图片的网址兄弟,请更新/自定义您的答案 –

+0

@DHEERAJ我试过了你的截图解决方案,但我没有得到我想要的图像。它只给我空白/白色图像。 –