I have an NSData object which I obtained from a web server.

the contents of this data object are supposed to be a UIImage . but when i used it in the following code :-

[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData * responseData, NSError *err) {

    if (err) {
        NSLog(@"Err %@",err.description);

    }else
    {

        if (responseData) 
        {
            NSLog(@"Data Length %d  ",[responseData length]);
            UIImage *img = [[UIImage alloc] initWithData:responseData];

            if (img) {
                NSLog(@"image in not null");
                self.imageView.image = img;
            }
            else
            {
                NSLog(@"image is null");
            }
        }

        else
        {
            NSLog(@"not returning anything");
        }
    }
}];

the out put says :-

Data Length 2786779
image is null

so i guess its not an Image

is there a way from which i could get to know which class instance does this NSData contains

PS:- I also used

NSLog("Description %@",data.description);

but it only generated a huge sequence of hex codes

有帮助吗?

解决方案

Well.... it might be possible that your server is sending an image but its encoded.

getting your UIImage *img = null doesn't necessarily mean that the response data is not an Image. if you aren't decoding it back, it will be null of course

I think your response data is encoded in Base64...(most servers do)

if so,...

first download .h and .m files from this Link

add and import them in your project.

then use this code

NSString *str = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
NSData *data = [NSData dataFromBase64String:str];
UIImage *img = [[UIImage alloc] initWithData:data];

this img will have your image. if your server is really sending an image.

please note that.. not all of NSString *str in this code is representation of a UIImage it might be some part of it. and the rest can be information related to this image. so you have to decode only the image part of the response.

hope this helps....

其他提示

A NSData object contains a sequence of bytes, not objects (e.g. UIImages). It can be a representation of an image, though.

You should check the "Content-Type" header of the response. It is a MIME type which specifies the kind of data in the response body (see also wiki Internet media type).

You can obtain the MIME type as a NSString from the NSURLResponseas follows:

NSString* mimeType = [response MIMEType];

(see also NSURLResponse Class Reference)

A MIME type is composed of a type and a subtype, and may contain "parameters".

For example, text/plain; charset=utf-8 is a media type whose "type" is "text" and whose "subtype" is "plain". It has one parameter specifying the character encoding of the byte sequence, which is in this case, Unicode UTF-8.

In your example, you would expect a MIME type whose "type" equals "image", for example:

image/gif, image/jpeg, image/png, etc.

I think your data is not data of a image. Use below code to log data as NSString and see result.

NSLog(@"%@",[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]);
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top