objective-c 从 URL 目标 C 获取图像
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/933099/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Getting Image from URL Objective C
提问by TheGambler
I'm trying to get an image from an URL and it doesn't seem to be working for me. Can someone point me in the right direction?
我正在尝试从 URL 获取图像,但它似乎对我不起作用。有人可以指出我正确的方向吗?
Here is my code:
这是我的代码:
NSURL *url = [NSURL URLWithString:@"http://myurl/mypic.jpg"];
NSString *newIMAGE = [[NSString alloc] initWithContentsOfURL:url
encoding:NSUTF8StringEncoding error:nil];
cell.image = [UIImage imageNamed:newIMAGE];
When I debug the newIMAGE string is nil so something isn't working there.
当我调试 newIMAGE 字符串是 nil 所以有些东西在那里不起作用。
回答by Jim Dovey
What you want is to get the image data, then initialize a UIImage using that data:
您想要的是获取图像数据,然后使用该数据初始化 UIImage:
NSData * imageData = [[NSData alloc] initWithContentsOfURL: [NSURL URLWithString: @"http://myurl/mypic.jpg"]];
cell.image = [UIImage imageWithData: imageData];
[imageData release];
As requested, here's an asynchronous version:
根据要求,这是一个异步版本:
dispatch_async(dispatch_get_global_queue(0,0), ^{
NSData * data = [[NSData alloc] initWithContentsOfURL: [NSURL URLWithString: @"http://myurl/mypic.jpg"]];
if ( data == nil )
return;
dispatch_async(dispatch_get_main_queue(), ^{
// WARNING: is the cell still using the same data by this point??
cell.image = [UIImage imageWithData: data];
});
[data release];
});
回答by Justicle
Ok there's a couple of things wrong here:
好吧,这里有一些错误:
The conversion from URL (url) to NSString (newImage) is incorrect, what the code actually does there is try to load the contents of "http://myurl/mypic.jpg" into the NSString.
The -imageNamed method takes a string that is a path of a local file, not a URL as an argument.
从 URL (url) 到 NSString (newImage) 的转换是不正确的,代码实际上所做的是尝试将“ http://myurl/mypic.jpg”的内容加载到 NSString 中。
-imageNamed 方法将一个字符串作为本地文件的路径,而不是 URL 作为参数。
You need to use an NSData object as an intermediary, like in this example: http://blogs.oreilly.com/digitalmedia/2008/02/creating-an-uiimage-from-a-url.html
您需要使用 NSData 对象作为中介,例如在此示例中:http: //blogs.oreilly.com/digitalmedia/2008/02/creating-an-uiimage-from-a-url.html
回答by tmr
the accepted answer asynchronous version worked very slow in my code. an approach using NSOperation worked light years faster. the code provided by Joe Masilotti --> objective - C : Loading image from URL?(and pasted below):
接受的答案异步版本在我的代码中运行速度非常慢。使用 NSOperation 的方法工作得更快。Joe Masilotti 提供的代码 -->目标 - C:从 URL 加载图像?(并粘贴在下面):
-(void) someMethod {
// set placeholder image
UIImage* memberPhoto = [UIImage imageNamed:@"place_holder_image.png"];
// retrieve image for cell in using NSOperation
NSURL *url = [NSURL URLWithString:group.photo_link[indexPath.row]];
[self loadImage:url];
}
- (void)loadImage:(NSURL *)imageURL
{
NSOperationQueue *queue = [NSOperationQueue new];
NSInvocationOperation *operation = [[NSInvocationOperation alloc]
initWithTarget:self
selector:@selector(requestRemoteImage:)
object:imageURL];
[queue addOperation:operation];
}
- (void)requestRemoteImage:(NSURL *)imageURL
{
NSData *imageData = [[NSData alloc] initWithContentsOfURL:imageURL];
UIImage *image = [[UIImage alloc] initWithData:imageData];
[self performSelectorOnMainThread:@selector(placeImageInUI:) withObject:image waitUntilDone:YES];
}
- (void)placeImageInUI:(UIImage *)image
{
[self.memberPhotoImage setImage:image];
}
回答by Frostmourne
Updating upon Jim dovey answer,[data release]is no longer required because in the updated apple guidelines. Memory management is done automatically by ARC (Automatic counting reference) ,
[data release]不再需要更新 Jim dovey 的答案,因为在更新的苹果指南中。内存管理由 ARC(自动计数参考)自动完成,
Here is the updated asynchronous call,
这是更新的asynchronous call,
dispatch_async(dispatch_get_global_queue(0,0), ^{
NSData * data = [[NSData alloc] initWithContentsOfURL: [NSURL URLWithString: @"your_URL"]];
if ( data == nil )
return;
dispatch_async(dispatch_get_main_queue(), ^{
self.your_UIimage.image = [UIImage imageWithData: data];
});
});
回答by Naishta
In Swift 3 and 4
在Swift 3 和 4 中
let theURL = URL(string:"https://exampleURL.com")
let imagedData = NSData(contentsOf: theURL!)!
let theImage = UIImage(data: imagedData as Data)
cell.theImageView.image = theImage
This will be done in the main thread.
这将在主线程中完成。
And to perform the same in asynchronous/background thread
并在异步/后台线程中执行相同的操作
DispatchQueue.main.async(){
let theURL = URL(string:"https://exampleURL.com")
let imagedData = NSData(contentsOf: theURL!)!
let theImage = UIImage(data: imagedData as Data)
}
cell.theImageView.image = theImage

