Failed to authenticate iPhone UIImage initWithData
I am trying to code an asynchronous image loader. I am using NSURLConnection to get data in NSMutableData and use that data after UIImage initialization is complete.
I checked bytes and it loads the whole image correctly (true at least the number of bytes); when i call
[UIImage imageWithData:data]
and then check the properties of the image, it is zero width and garbage number for height, in fact, the same number no matter what the image is. I tried with many different images, png, jpg, different urls, it always downloads the whole image, but UIImage cannot initialize this data. What could I be doing wrong here?
Thanks.
The code is indeed what you expect it to look like:
Delegate connections:
-(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 source to share
First, I'm pretty sure the UIImage won't initialize with garbage data. The constructor initWithData
analyzes the data to determine the file format. If your data is corrupted, the returned image will be null. Check it out 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];
}
Secondly, make sure you add data during the upload process. Here is the callback method:
- (void) connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
[self.receivedData appendData:data];
}
Finally, your code must make sure to include the second case: download failure.
- (void) connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
[connection release];
}
The problem description is missing some important parts of the code.
Apple's URLCache demo is a very good project for understanding async image loading. http://developer.apple.com/iphone/library/samplecode/URLCache/Introduction/Intro.html
I hope this helps you!
a source to share
I cannot tell what is wrong, maybe you are not using the release properly, tell me I donβt know why you release the connection at the end of dataDownloadedBy, should it be an image? it might help if you post your code here.
I did the same, you can see the post here http://blog.163.com/ lionyue @ 126 / blog / static / 1079307120096895433277 /
Hope it helps
a source to share