iPad: ImageIO: JPEGNot a JPEG file: starts

2019-08-09 23:56发布

问题:

the error-message i quoted in question-title is driving me crazy on my iPad.

i am downloading some Jpeg-Files form HTTP-Servers, and using my code works fine on the iphone (iOS 5.0). On my iPad (4.3) it messages this error and the image cannot be displayed.

What makes me wonder is how i fixed it, which is not clear to me!

So i am Downloading the Jpeg via NSURLConnection (conn) and a NSMutableData (imageData):

conn = [[NSURLConnection alloc] 
       initWithRequest:[NSURLRequest requestWithURL:[camera getJpgUrl] 
                                        cachePolicy:NSURLRequestReloadIgnoringLocalCacheData 
                                    timeoutInterval:10] 
                       delegate:self 
                       startImmediately:YES];

if(conn && !imageData)
{
    imageData = [[NSMutableData data] retain];
}

the delegate-methods are :

1) getting more data

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

2) having all the data:

-(void)connectionDidFinishLoading:(NSURLConnection *)aConnection
{
    UIImage *img = [UIImage imageWithData:imageData];

    [imageData setLength:0];
    [conn release];
    conn = nil;

    if(img)
    {
        imageView.image=img;
    }
}

Hooray sais the iphone, welcome yells the image.

On the iPad however, i have to release the imageData-Object instead of settings its length to 0 when i restart that method initializing the NSUrlConnection. i restart it for image-updates etc.

So the code in iPad is (just to sum it up):

-(void)connectionDidFinishLoading:(NSURLConnection *)aConnection
{
    UIImage *img = [UIImage imageWithData:imageData];

    [imageData release];
    imagedata = [[NSMutableData data] retain];
    conn release];
    conn = nil;

    if(img)
    {
        imageView.image=img;
    }
}

So for now, the imageData handling in my code is a result of testing, so i could change it to have a move elegant handling. Thats not so important.

But what makes me wonder is, why does setLength:0 does what it should on ios5/iphone4, but not on ipad? i hope that i just messed up something, but here i really dont understand what is going on.

Any thoughts ?

Thank you!

回答1:

okay, the server hat some bugs :/