Getting Image from URL/server [duplicate]

2019-01-22 01:10发布

问题:

This question already has an answer here:

  • iOS download and save image inside app 10 answers

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

回答1:

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.



回答2:

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/



回答3:

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.



回答4:

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.



回答5:

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];
}