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 8678385
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T20:42:26+00:00 2026-06-12T20:42:26+00:00

I am trying to convert my UIView into UIImage using below code. + (UIImage

  • 0

I am trying to convert my UIView into UIImage using below code.

+ (UIImage *) imageWithView:(UIView *)view{
     float scale = 1.0f;
    UIGraphicsBeginImageContextWithOptions(view.bounds.size, YES, scale);
    [view.layer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage* img = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    view.layer.contents = nil;
    return img;
}

There are two problem with this code.

1. When I run this code in background thread(!mainThread)

I faced memory leak problem when renderInContext is called in background thread.

2. When I run this code on Main thread

There is no memory leak but on iPad 3 I am facing some performance issue (my UI hangs when this method is called) while creating image from UIView. As I need to call this function more than 5 times in a seconds so UI hangs gives very bad user experience.

Please guide me if I am doing some thing wrong here?

  • 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-12T20:42:27+00:00Added an answer on June 12, 2026 at 8:42 pm

    I think that issue 1 is related to the fact that UIKit is not thread safe and its use will lead to all kinds of side effects.

    If you have performance issue like you are describing, the only path forward I see is directly using CoreGraphics (not UIKit) on a secondary thread.

    You might try something like this, as a start:

    size_t width = view.bounds.size.width;
    size_t height = view.bounds.size.height;
    
    unsigned char *imageBuffer = (unsigned char *)malloc(width*height*4);
    CGColorSpaceRef colourSpace = CGColorSpaceCreateDeviceRGB();
    
    CGContextRef imageContext =
        CGBitmapContextCreate(imageBuffer, width, height, 8, width*4, colourSpace,
                  kCGImageAlphaPremultipliedFirst | kCGBitmapByteOrder32Little);
    
    CGColorSpaceRelease(colourSpace);
    
    [view.layer renderInContext:imageContext];
    
    CGImageRef outputImage = CGBitmapContextCreateImage(imageContext);
    
    CGImageRelease(outputImage);
    CGContextRelease(imageContext);
    free(imageBuffer);
    

    As you see, this is pretty more complex than the UIKit way, but it can be run on a secondary thread (provided you find a way to pass the outputImage back to your UI thread which is not shown).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to convert the code below into a parallel loop. What is
Trying to convert this c code into MIPS and run it in SPIM. int
I'm using lambda expression and trying convert into uint while adding into the Hashset.
Trying to convert a JSON string into an object in C#. Using a really
I'm trying convert all special chars into HTML safe entities on their way into
i´m using subsonic 3 trying convert a SQL2008 project to MySQL. when the projects
Trying to convert a string of binary inputs into a vector of ints. I'd
Trying to convert my local SVN repo to Git using svn2git as suggested by
im trying to convert a date format into a new format in this example
I'm trying convert xml from one format to other using the XslCompiledTransform in c#.

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.