从 URL/服务器获取图像
我对 iPhone 开发相当陌生,并尝试在我的应用程序中获取“来自服务器的图像”。
我使用以下方法来执行此操作:
- (UIImage *)imageFromURLString:(NSString *)urlString
{
NSURL *url = [NSURL URLWithString:urlString];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:url];
[request setHTTPMethod:@"GET"];
NSURLResponse *response = nil;
NSError *error = nil;
NSData *result = [NSURLConnection sendSynchronousRequest:request
returningResponse:&response error:&error];
[request release];
[self handleError:error];
UIImage *resultImage = [UIImage imageWithData:(NSData *)result];
NSLog(@"urlString: %@",urlString);
return resultImage;
}
尽管我可以在调试器中看到图像对象的 NSData 有一些值(以字节为单位),但此函数不会返回预期的图像
尽管,用于从服务器获取文本的非常相似的函数也有效我得到了预期值。
- (NSString *)jsonFromURLString:(NSString *)urlString
{
NSURL *url = [NSURL URLWithString:urlString];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:url];
[request setHTTPMethod:@"GET"];
NSURLResponse *response = nil;
NSError *error = nil;
NSData *result = [NSURLConnection sendSynchronousRequest:request
returningResponse:&response error:&error];
[request release];
[self handleError:error];
NSString *resultString = [[NSString alloc] initWithData:result
encoding:NSUTF8StringEncoding];
return [resultString autorelease];
}
这个方法效果很好。
有人可以帮助我理解为什么我没有从服务器获取图像吗?
谢谢
I'm fairly new to iPhone development and trying to fetch 'Images from Server' in my application.
I'm using the following method to do this:
- (UIImage *)imageFromURLString:(NSString *)urlString
{
NSURL *url = [NSURL URLWithString:urlString];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:url];
[request setHTTPMethod:@"GET"];
NSURLResponse *response = nil;
NSError *error = nil;
NSData *result = [NSURLConnection sendSynchronousRequest:request
returningResponse:&response error:&error];
[request release];
[self handleError:error];
UIImage *resultImage = [UIImage imageWithData:(NSData *)result];
NSLog(@"urlString: %@",urlString);
return resultImage;
}
This function does not return the expected image although I can see in debugger that NSData for the Image object has some value (in bytes)
Although, a very similar function for getting text from server works and I get expected values.
- (NSString *)jsonFromURLString:(NSString *)urlString
{
NSURL *url = [NSURL URLWithString:urlString];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:url];
[request setHTTPMethod:@"GET"];
NSURLResponse *response = nil;
NSError *error = nil;
NSData *result = [NSURLConnection sendSynchronousRequest:request
returningResponse:&response error:&error];
[request release];
[self handleError:error];
NSString *resultString = [[NSString alloc] initWithData:result
encoding:NSUTF8StringEncoding];
return [resultString autorelease];
}
This method works fine.
Could someone please help me in understanding why I'm not getting the Image from server?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
如果您只想通过 URL 从 Web 服务器获取图像,那么还有一种更简单的方法。
这就是我使用的:
如果你想在图像视图上使用它,只需执行以下操作:
我确信有人可能不想使用这种方法是有原因的,但我会从这个开始,看看是否可以会做你需要它做的事。
If all you are trying to do is grab an image from a web server via a URL there is an easier way.
This is what I use:
And if you want to use it on an image view just do the following:
I'm sure there are reasons why a person might not want to use this approach, but I would start with this and see if it will do what you need it to.
我编写了 RemoteImage 类来通过网络异步加载图像。它还负责在必要时释放内存。请参阅这篇文章:http://www.dimzzy.com /blog/2009/11/remote-image-for-iphone/
I've written RemoteImage class to load images over the network asynchronously. It also takes care of freeing the memory when necessary. See this post: http://www.dimzzy.com/blog/2009/11/remote-image-for-iphone/
最可能的原因是您实际上并未获取图像,或者您正在获取 iPhone 无法解码的图像。我首先检查结果的 MIME 类型 (
[response MIMEType]
),并确保它是您所期望的,而不是实际的字符串。然后确保您的类型是 UIImage 可以处理:tiff、jpeg、gif、png、bmp、ico、cur、xbm。The most likely cause is that you're not actually fetching an image, or you are fetching an image that iPhone can't decode. I would start by checking the MIME type of the result (
[response MIMEType]
) and make sure it's what you're expecting and not actually a string for instance. Then make sure that your type is one of the types UIImage can handle: tiff, jpeg, gif, png, bmp, ico, cur, xbm.很可能您的服务器返回的实际上并不是图像数据,您只需将 URL 粘贴到浏览器中即可查看是否获得图像。除了缺乏错误检查之外,代码似乎没有任何问题。
当您将非图像 NSData 传递到 +imageWithData 消息时,您将得到 nil。你也许应该检查一下。
It could very well be that what your server're returning isn't actually image data, and you can just paste the url into a browser and see if you get the image. There doesn't seem to be anything wrong with the code except for the lack of error-checking.
When you pass a non-image NSData into a +imageWithData message, you will get nil. You should probably check for that.
它可能与您的问题没有直接关系,但我确实想指出您此处的错误检查存在潜在问题。像这样的方法有一个 NSError 的引用参数,通常不会定义成功时返回的内容。换句话说,您应该首先检查方法的返回值,并且仅在方法失败时访问错误指针:
It probably isn't directly relevant to your question, but I did want to point out that your error checking here has a potential issue. Methods like this that have a reference parameter to an
NSError
generally do not define what they return on success. In other words, you should check the methods return value first, and only access the error pointer if the method failed: