I am checking the dimensions of images via their URLs stored in an array called destination_urls. I then calculate the height of images if they were stretched to the width of the screen while maintaining the aspect ratio. All of this is done in a for loop.
When the code is run, the app UI gets stuck during the for loop. How can I revise the code to make sure the app doesn't freeze?
int t = 0;
int arraycount = [_destination_urls count];
dispatch_async(dispatch_get_main_queue(), ^{
for (int i = 0; i < arraycount; i++) {
NSURL *imageURL = [NSURL URLWithString:[_destination_urls[i] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
CGImageSourceRef imgSource = CGImageSourceCreateWithURL((__bridge CFURLRef)imageURL, NULL);
NSDictionary* imageProps = (__bridge_transfer NSDictionary*) CGImageSourceCopyPropertiesAtIndex(imgSource, 0, NULL);
NSString *imageHeighttoDisplay = [imageProps objectForKey:@"PixelHeight"];
originalimageHeight = [imageHeighttoDisplay intValue];
NSString *imageWidthtoDisplay = [imageProps objectForKey:@"PixelWidth"];
originalimageWidth = [imageWidthtoDisplay intValue];
if (imgSource){
_revisedImageURLHeight[t] = [NSNumber numberWithInt:(screenWidth)*(originalimageHeight/originalimageWidth)];
t = t +1;
CFRelease(imgSource);
}
}
});