Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8007189
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T17:44:49+00:00 2026-06-04T17:44:49+00:00

Let me tell you about the problem I am having and how I tried

  • 0

Let me tell you about the problem I am having and how I tried to solve it. I have a UIScrollView which loads subviews as one scrolls from left to right. Each subview has 10-20 images around 400×200 each. When I scroll from view to view, I experience quite a bit of lag.

After investigating, I discovered that after unloading all the views and trying it again, the lag was gone. I figured that the synchronous caching of the images was the cause of the lag. So I created a subclass of UIImageView which loaded the images asynchronously. The loading code looks like the following (self.dispatchQueue returns a serial dispatch queue).

- (void)loadImageNamed:(NSString *)name {
    dispatch_async(self.dispatchQueue, ^{
        UIImage *image = [UIImage imageNamed:name];

        dispatch_sync(dispatch_get_main_queue(), ^{
            self.image = image;
        });
    });
}

However, after changing all of my UIImageViews to this subclass, I still experienced lag (I’m not sure if it was lessened or not). I boiled down the cause of the problem to self.image = image;. Why is this causing so much lag (but only on the first load)?

Please help me. =(

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T17:44:51+00:00Added an answer on June 4, 2026 at 5:44 pm

    EDIT 3: iOS 15 now offers UIImage.prepareForDisplay(completionHandler:).

    imageView.image = await image.byPreparingForDisplay()
    

    or

    image.prepareForDisplay { decodedImage in
        DispatchQueue.main.async {
            imageView.image = decodedImage
        }
    }
    

    EDIT 2: Here is a Swift version that contains a few improvements. (Untested.)
    https://gist.github.com/fumoboy007/d869e66ad0466a9c246d


    EDIT: Actually, I believe all that is necessary is the following. (Untested.)

    - (void)loadImageNamed:(NSString *)name {
        dispatch_async(self.dispatchQueue, ^{
            // Determine path to image depending on scale of device's screen,
            // fallback to 1x if 2x is not available
            NSString *pathTo1xImage = [[NSBundle mainBundle] pathForResource:name ofType:@"png"];
            NSString *pathTo2xImage = [[NSBundle mainBundle] pathForResource:[name stringByAppendingString:@"@2x"] ofType:@"png"];
    
            NSString *pathToImage = ([UIScreen mainScreen].scale == 1 || !pathTo2xImage) ? pathTo1xImage : pathTo2xImage;
    
    
            UIImage *image = [[UIImage alloc] initWithContentsOfFile:pathToImage];
    
            // Decompress image
            if (image) {
                UIGraphicsBeginImageContextWithOptions(image.size, NO, image.scale);
    
                [image drawAtPoint:CGPointZero];
    
                image = UIGraphicsGetImageFromCurrentImageContext();
    
                UIGraphicsEndImageContext();
            }
    
    
            // Configure the UI with pre-decompressed UIImage
            dispatch_async(dispatch_get_main_queue(), ^{
                self.image = image;
            });
        });
    }
    

    ORIGINAL ANSWER: It turns out that it wasn’t self.image = image; directly. The UIImage image loading methods don’t decompress and process the image data right away; they do it when the view refreshes its display. So the solution was to go a level lower to Core Graphics and decompress and process the image data myself. The new code looks like the following.

    - (void)loadImageNamed:(NSString *)name {
        dispatch_async(self.dispatchQueue, ^{
            // Determine path to image depending on scale of device's screen,
            // fallback to 1x if 2x is not available
            NSString *pathTo1xImage = [[NSBundle mainBundle] pathForResource:name ofType:@"png"];
            NSString *pathTo2xImage = [[NSBundle mainBundle] pathForResource:[name stringByAppendingString:@"@2x"] ofType:@"png"];
            
            NSString *pathToImage = ([UIScreen mainScreen].scale == 1 || !pathTo2xImage) ? pathTo1xImage : pathTo2xImage;
            
            
            UIImage *uiImage = nil;
            
            if (pathToImage) {
                // Load the image
                CGDataProviderRef imageDataProvider = CGDataProviderCreateWithFilename([pathToImage fileSystemRepresentation]);
                CGImageRef image = CGImageCreateWithPNGDataProvider(imageDataProvider, NULL, NO, kCGRenderingIntentDefault);
                
                
                // Create a bitmap context from the image's specifications
                // (Note: We need to specify kCGImageAlphaPremultipliedFirst | kCGBitmapByteOrder32Little
                // because PNGs are optimized by Xcode this way.)
                CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
                CGContextRef bitmapContext = CGBitmapContextCreate(NULL, CGImageGetWidth(image), CGImageGetHeight(image), CGImageGetBitsPerComponent(image), CGImageGetWidth(image) * 4, colorSpace, kCGImageAlphaPremultipliedFirst | kCGBitmapByteOrder32Little);
                
                
                // Draw the image into the bitmap context
                CGContextDrawImage(bitmapContext, CGRectMake(0, 0, CGImageGetWidth(image), CGImageGetHeight(image)), image);
                
                //  Extract the decompressed image
                CGImageRef decompressedImage = CGBitmapContextCreateImage(bitmapContext);
                
                
                // Create a UIImage
                uiImage = [[UIImage alloc] initWithCGImage:decompressedImage];
                
                
                // Release everything
                CGImageRelease(decompressedImage);
                CGContextRelease(bitmapContext);
                CGColorSpaceRelease(colorSpace);
                CGImageRelease(image);
                CGDataProviderRelease(imageDataProvider);
            }
            
            
            // Configure the UI with pre-decompressed UIImage
            dispatch_async(dispatch_get_main_queue(), ^{
                self.image = uiImage;
            });
        });
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Greetings, Here's the problem I'm having. I have a page which redirects directly to
i' a new iphone developer coming from flash stuff. let me tell you about
First let me tell you what my situation is I have 3 service Providers
First of all let me tell you that i have read the following questions
Let's say I have a method in java, which looks up a user in
In my view this is one of the strangest problem i have ever come
I have a table which needs 2 fields. One will be a foreign key,
I have 2 questions, but they are about the same (similar?) problem. First question:
the last days I have researched about cron jobs. First I want to tell
First let me tell about my app scenario. UINavigationController {relationship} UIViewControllerMain {push} UITabController {push}

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.