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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T03:14:41+00:00 2026-05-31T03:14:41+00:00

Trying to create a UIimage from a Draw Context. Not seeing anything. Am i

  • 0

Trying to create a UIimage from a Draw Context.
Not seeing anything. Am i missing something, or completely out my mind?

Code

- (UIImage *)drawRect:(CGRect)rect {

    CGContextRef context = UIGraphicsGetCurrentContext();

    CGContextMoveToPoint(context, 100, 100);
    CGContextAddLineToPoint(context, 150, 150);
    CGContextAddLineToPoint(context, 100, 200);
    CGContextAddLineToPoint(context, 50, 150);
    CGContextAddLineToPoint(context, 100, 100);

    CGContextSetFillColorWithColor(context, [UIColor redColor].CGColor);
    CGContextFillPath(context);


    // Do your stuff here
    CGImageRef imgRef = CGBitmapContextCreateImage(context);
    UIImage* img = [UIImage imageWithCGImage:imgRef];
    CGImageRelease(imgRef);
    CGContextRelease(context);
    return img;
}
  • 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-05-31T03:14:42+00:00Added an answer on May 31, 2026 at 3:14 am

    I’m assuming this is not a -drawRect: method on a view, because the return value is wrong. (-[UIView drawRect:] returns void, not a UIImage*.)

    If it is on an NSView, that means you must be calling it directly, to get the return value. But that means that UIKit hasn’t set up a graphics context, the way it normally does before it calls -drawRect: on the views in a window.

    Therefore, you shouldn’t assume that UIGraphicsGetCurrentContext() is valid. It’s probably nil (have you checked?).

    If you just want an image: use UIGraphicsBeginImageContext() to create a context, then UIGraphicsGetImageFromCurrentImageContext() to extract a UIImage (no need for the intermediary CGImage), then UIGraphicsEndImageContext() to clean up.

    If you’re trying to capture an image of what your view drew: fix your -drawRect: to return void, and find some other way to get that UIImage out of the view — either stash it in an ivar, or send it to some other object, or write it to a file, whatever you like.

    Also (less importantly):

    1. Don’t CGContextRelease(context). You didn’t create, copy, or retain it, so you shouldn’t release it.

    2. No need for the last CGContextAddLineToPoint(). CGContextFillPath will implicitly close the path for you.

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

Sidebar

Related Questions

I am trying to create a UIImage from a byte array that is actually
Trying to create Database as follows: USE Master GO IF NOT EXISTS(SELECT [Name] FROM
I am trying to create a TIFF image from a UIImage . I looked
I'm a trying to create a gray IplImage from a gray scaled UIImage and
I'm trying to create custom view that draws image downloaded from Url. The code
I'm trying to create an UIImage test pattern for an iOS 5.1 device. The
As the title says, I'm trying to create a UIImage that will go inside
I'm trying to display a UIImage in real-time coming from the camera, and it
I am trying to programatically create an image from the current frame of a
I’m trying to create thumbnails for video files: - (UIImage*) thumbnailForVideoAtURL: (NSURL*) videoURL {

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.