Skip to content

Instantly share code, notes, and snippets.

@ThinhPhan
Created July 8, 2015 17:13
Show Gist options
  • Save ThinhPhan/077bcc1ab49d33ad7a4b to your computer and use it in GitHub Desktop.
Save ThinhPhan/077bcc1ab49d33ad7a4b to your computer and use it in GitHub Desktop.
Check image get from URL is valid or corrupted or URL return another type of data (JSON, garbage data ...)
- (void)isImageDataValid:(NSURL *)imageURL
{
dispatch_async(dispatch_queue_create("CheckImage", NULL), ^{
NSData *data = [NSData dataWithContentsOfURL:imageURL];
UIImage *image = [UIImage imageWithData:data];
if (image == nil) {
// imageURL is not valid
TLog(@"imageURL NOT valid: %@", imageURL);
}
else {
// imageURL is valid
TLog(@"imageURL VALID: %@", imageURL);
dispatch_async(dispatch_get_main_queue(), ^{
UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil);
//show your image
});
}
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment