tags:

views:

323

answers:

3

Hello all,

I'm trying to code up an async image downloader. I use NSURLConnection to get the data into an NSMutableData and use that data once it is complete to initialize a UIImage.

I checked the bytes and it downloads the entire image correctly (right number of bytes at least), however; when I call

[UIImage imageWithData:data]

and then check the properties of the image, it is zero width and a garbage number for height, in fact, same number no matter what the image is. I tried with bunch of different images, png, jpg, different urls, it always downloads the image completely but UIImage can't initialize with that data. What could I be doing wrong here?

Thanks.

Code is really as you'd expect it to look like:

Connection Delegate:

-(void)connectionDidFinishLoading:(NSURLConnection*)theConnection {
[[ImageManager sharedInstance] dataDownloadedBy:self]; }

ImageManager:

-(void)dataDownloadedBy:(WebConnection *)connection{    
WebImage *image = [[WebImage alloc] initWithLink:connection.url];
[image setImageFromData:connection.data];
[images addObject:image];
[connection release];}

WebImage:

-(void)setImageFromData:(NSMutableData *)data{
image = [[UIImage alloc] initWithData:data];}
A: 

I can't tell what is wrong, it could be you don't use the release properly, say i don't know why you release connection at the end of dataDownloadedBy, it is supposed to be image ? it could help if you post more your code here.

I used to do the same thing, you can have a look the post here http://blog.163.com/lionyue@126/blog/static/1079307120096895433277/

Hope it helps

owen
A: 

If initWithData is failing, most likely the image data you're getting is corrupt. You should save it to a file like this:

[data writeToFile:@"/tmp/foo.jpg" atomically:NO];

and then try to open it in Preview.app.

Ken Aspeslagh
+1  A: 

First, I'm sure the UIImage will not initialize with garbage data. The constructor initWithData analyzes the data to determine the file format. If your data is corrupted, the image returned will be nil. Check this at first.

-(void)dataDownloadedBy:(WebConnection *)connection{    
    WebImage *image = [[WebImage alloc] initWithLink:connection.url];
    [image setImageFromData:connection.data];
    if (image.image != nil) { [images addObject:image]; }
    [connection release];
}

Second, make sure you append the data during the download process. Here is the callback method:

- (void) connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
    [self.receivedData appendData:data];
}

Finally, your code must absolutely includes the second case: a download failure.

- (void) connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
    [connection release];
}

Your problem description lacks some important pieces of code.

The URLCache demo from Apple is a very good project to understand async image download. http://developer.apple.com/iphone/library/samplecode/URLCache/Introduction/Intro.html

I hope this will help you!

rjobidon