Search code examples
iosuiimagensdata

NSData initWithContentsOfURL reading not all data, but only on device


I am banging my head about an issue I have on iOS7 development. I use the following piece of code to load an image from a webserver:

    NSData* data = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:@"http://someServer/someImage.jpg"]];

This works like a charme in simulator, reading exactly the 134185 bytes that the image has. Creating an UIImage from that data works as intended. Once I test the exact same code on a device (iPad Mini, iOS 7.03), though, it just reads 14920 byte from the same URL. Needless to say that I can't create an UIImage from that data then, creation fails and returns a nil. The read does not produce any errors (no console output, and also using the signature with the error output param returns nil here). Is there anything I missed around this rather straightforward task? Haven't found anything on the web on this…

Thanks, habitoti


Solution

  • So you don't have any error, and something is downloading. Maybe try to read this response and post here (I guess it is html/text body)?
    You can use NSString method:
    + (instancetype)stringWithContentsOfURL:(NSURL )url encoding:(NSStringEncoding)enc error:(NSError *)error;