从 iPhone 从 URL 加载图像
我正在将图像从 url 加载到 webviews 中,但这向我显示了被删减的图像。我尝试执行 sizeToFit,但这在我的网页视图中显示了一个非常小的图像,位于左侧角,因为网页很大并且图像位于其左上角。
编辑
这就是我在网络视图中加载图像的方式。这里,expanded_photo 是 webview。
NSString *urlAddress = [NSString stringWithFormat:@"%@",photo_url];
NSURL *url = [NSURL URLWithString:urlAddress];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[expanded_photo loadRequest:requestObj];
每当我尝试使用如下所示的 uiimage 通过 uiimageview 加载它时,这里 Expanded_photo 是我在 nib 文件中创建的 imageview:
UIImage *image1 = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:photo_url]]];
expanded_photo.image = image1;
这需要很多时间来加载。
我想要一个可以在短时间内从 url 加载图像并且图像不会被剪切的解决方案。
I'm loading images from url into webviews but that's showing me images which are cut down. I tried doing sizeToFit, but that shows a very small image cornered at left in my webview as the webage is large and image at its upper left corner.
Edit
This is how I'm loading images in webview. Here, expanded_photo is webview.
NSString *urlAddress = [NSString stringWithFormat:@"%@",photo_url];
NSURL *url = [NSURL URLWithString:urlAddress];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[expanded_photo loadRequest:requestObj];
Whenever I try loading it through uiimageview using a uiimage like following, Here expanded_photo is imageview which I'm creating in a nib file:
UIImage *image1 = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:photo_url]]];
expanded_photo.image = image1;
This takes a lot of time to load.
I want a solution which can load image from url in a small amount of time and the image is not cut.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
请注意, -[NSData dataWithContentsOfURL:] 同步加载数据,因此会阻塞主线程,直到下载完成。当您以这种方式加载多个图像时,所有这些加载操作都会按顺序执行。这不仅很慢,而且您的应用程序在加载过程中没有响应。
您可能会考虑在后台线程中移动实际的加载请求(使用 NSOperationQueue)或使用 NSURLConnection 异步加载图像。
Be aware that -[NSData dataWithContentsOfURL:] loads the data synchronously and thus blocks your main thread until the download is finished. When you load multiple images that way, all those loading operations are executed sequentially. This is not only slow but also your app is unresponsive during loading.
You might consider moving the actual load request in a background thread (using NSOperationQueue) or use NSURLConnection to asynchronously load the image.