从 URL/服务器获取图像

发布于 2024-08-02 05:30:42 字数 1505 浏览 3 评论 0原文

我对 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

三人与歌 2024-08-09 05:30:42

如果您只想通过 URL 从 Web 服务器获取图像,那么还有一种更简单的方法。

这就是我使用的:

UIImage* myImage = [UIImage imageWithData: 
    [NSData dataWithContentsOfURL: 
    [NSURL URLWithString: @"http://example.com/image.jpg"]]];

如果你想在图像视图上使用它,只需执行以下操作:

// Let's assume the picture is an instantiated UIImageView
[picture setImage: myImage];
[myImage release];

我确信有人可能不想使用这种方法是有原因的,但我会从这个开始,看看是否可以会做你需要它做的事。

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:

UIImage* myImage = [UIImage imageWithData: 
    [NSData dataWithContentsOfURL: 
    [NSURL URLWithString: @"http://example.com/image.jpg"]]];

And if you want to use it on an image view just do the following:

// Let's assume the picture is an instantiated UIImageView
[picture setImage: myImage];
[myImage release];

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.

多情癖 2024-08-09 05:30:42

我编写了 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/

舞袖。长 2024-08-09 05:30:42

最可能的原因是您实际上并未获取图像,或者您正在获取 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.

花开半夏魅人心 2024-08-09 05:30:42

很可能您的服务器返回的实际上并不是图像数据,您只需将 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.

杯别 2024-08-09 05:30:42

它可能与您的问题没有直接关系,但我确实想指出您此处的错误检查存在潜在问题。像这样的方法有一个 NSError 的引用参数,通常不会定义成功时返回的内容。换句话说,您应该首先检查方法的返回值,并且在方法失败时访问错误指针:

NSString *resultString = nil;
NSData *result = [NSURLConnection sendSynchronousRequest:request
                                       returningResponse:&response
                                                   error:&error];
[request release];

if( result ) {
  resultString = [[NSString alloc] initWithData:result
                                       encoding:NSUTF8StringEncoding];
}
else {
  [self handleError:error];
}

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:

NSString *resultString = nil;
NSData *result = [NSURLConnection sendSynchronousRequest:request
                                       returningResponse:&response
                                                   error:&error];
[request release];

if( result ) {
  resultString = [[NSString alloc] initWithData:result
                                       encoding:NSUTF8StringEncoding];
}
else {
  [self handleError:error];
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文