1

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);
    }


}

});
  • 1
    try moving the code off the main queue and onto a background queue. `dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{ });` – Dennis W. Jun 16 '16 at 02:56
  • The UI doesn't get blocked anymore but it causes multiple entries in _revisedImageURLHeight[t] for the same image. I did an NSLog for _revisedImageURLHeight and it shows the output as 199, 199, 199, 179, 179.. etc. The 199 is for a single image and 179 is for another. – user2533040 Jun 19 '16 at 02:11

0 Answers0