NSError *error = nil;
NSURL *url = [NSURL URLWithString:[imageLinks objectAtIndex:0]];
NSData *tdata = [NSData dataWithContentsOfURL:url options:NSDataReadingUncached error:&error];
if (error) {
NSLog(@"%@", [error localizedDescription]);
}else {
// no error, this one is being called within my app
NSLog(@"Data loaded successfully");
}
self.pictureView1.image = [UIImage imageWithData:tdata];
I have a jpeg file, I can confirm that the URL content is gathered successfully, but when I try to put my image data into the UIImage
my app fails. I wonder if there is any check for NSData to confirm it is usable for UIImage
.
I also don't know what causes this failure, and how to prevent it. Any help would be appreciated.
UIImage contains the data for an image. UIImageView is a custom view meant to display the UIImage . Save this answer.
Once a UIImage is created, the image data is loaded into memory and no longer connected to the file on disk. As such, the file can be deleted or modified without consequence to the UIImage and there is no way of getting the source path from a UIImage.
An object that manages image data in your app.
As stated in the documentation imageWithData: returns nil if UIImage could not create an image from the data. The correct way to handle this at runtime is to provide a placeholder image when that method returns nil.
As for the diagnostic, first look at the console messages, they sometimes provide useful info. If not, your best luck is to dump the NSData to disk and compare the result to the file stored on the server. You sometimes get corrupted data, you sometimes just get an html error message where you were expecting jpeg binary data.
You can use
if ([UIImage imageWithData:yourNSData]) {
// there was an image
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With