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

  • SEARCH
  • Home
  • 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 158887
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T10:45:40+00:00 2026-05-11T10:45:40+00:00

I try to get rounded corners on a UIImage, what I read so far,

  • 0

I try to get rounded corners on a UIImage, what I read so far, the easiest way is to use a mask images. For this I used code from TheElements iPhone Example and some image resize code I found. My problem is that resizedImage is always nil and I don’t find the error…

- (UIImage *)imageByScalingProportionallyToSize:(CGSize)targetSize {     CGSize imageSize = [self size];     float width = imageSize.width;     float height = imageSize.height;      // scaleFactor will be the fraction that we'll     // use to adjust the size. For example, if we shrink     // an image by half, scaleFactor will be 0.5. the     // scaledWidth and scaledHeight will be the original,     // multiplied by the scaleFactor.     //     // IMPORTANT: the 'targetHeight' is the size of the space     // we're drawing into. The 'scaledHeight' is the height that     // the image actually is drawn at, once we take into     // account the ideal of maintaining proportions      float scaleFactor = 0.0;      float scaledWidth = targetSize.width;     float scaledHeight = targetSize.height;      CGPoint thumbnailPoint = CGPointMake(0,0);      // since not all images are square, we want to scale     // proportionately. To do this, we find the longest     // edge and use that as a guide.      if ( CGSizeEqualToSize(imageSize, targetSize) == NO )     {          // use the longeset edge as a guide. if the         // image is wider than tall, we'll figure out         // the scale factor by dividing it by the         // intended width. Otherwise, we'll use the         // height.          float widthFactor = targetSize.width / width;         float heightFactor = targetSize.height / height;          if ( widthFactor < heightFactor )             scaleFactor = widthFactor;         else             scaleFactor = heightFactor;          // ex: 500 * 0.5 = 250 (newWidth)          scaledWidth = width * scaleFactor;         scaledHeight = height * scaleFactor;          // center the thumbnail in the frame. if         // wider than tall, we need to adjust the         // vertical drawing point (y axis)          if ( widthFactor < heightFactor )             thumbnailPoint.y = (targetSize.height - scaledHeight) * 0.5;          else if ( widthFactor > heightFactor )             thumbnailPoint.x = (targetSize.width - scaledWidth) * 0.5;     }       CGContextRef mainViewContentContext;     CGColorSpaceRef colorSpace;      colorSpace = CGColorSpaceCreateDeviceRGB();      // create a bitmap graphics context the size of the image     mainViewContentContext = CGBitmapContextCreate (NULL, targetSize.width, targetSize.height, 8, 0, colorSpace, kCGImageAlphaPremultipliedLast);      // free the rgb colorspace     CGColorSpaceRelease(colorSpace);          if (mainViewContentContext==NULL)         return NULL;      //CGContextSetFillColorWithColor(mainViewContentContext, [[UIColor whiteColor] CGColor]);     //CGContextFillRect(mainViewContentContext, CGRectMake(0, 0, targetSize.width, targetSize.height));      CGContextDrawImage(mainViewContentContext, CGRectMake(thumbnailPoint.x, thumbnailPoint.y, scaledWidth, scaledHeight), self.CGImage);      // Create CGImageRef of the main view bitmap content, and then     // release that bitmap context     CGImageRef mainViewContentBitmapContext = CGBitmapContextCreateImage(mainViewContentContext);     CGContextRelease(mainViewContentContext);      CGImageRef maskImage = [[UIImage imageNamed:@'Mask.png'] CGImage];      CGImageRef resizedImage = CGImageCreateWithMask(mainViewContentBitmapContext, maskImage);     CGImageRelease(mainViewContentBitmapContext);      // convert the finished resized image to a UIImage      UIImage *theImage = [UIImage imageWithCGImage:resizedImage];      // image is retained by the property setting above, so we can      // release the original     CGImageRelease(resizedImage);      // return the image     return theImage; } 
  • 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. 2026-05-11T10:45:41+00:00Added an answer on May 11, 2026 at 10:45 am

    The problem was the use of CGImageCreateWithMask which returned an all black image. The solution I found was to use CGContextClipToMask instead:

    CGContextRef mainViewContentContext; CGColorSpaceRef colorSpace;  colorSpace = CGColorSpaceCreateDeviceRGB();  // create a bitmap graphics context the size of the image mainViewContentContext = CGBitmapContextCreate (NULL, targetSize.width, targetSize.height, 8, 0, colorSpace, kCGImageAlphaPremultipliedLast);  // free the rgb colorspace CGColorSpaceRelease(colorSpace);      if (mainViewContentContext==NULL)     return NULL;  CGImageRef maskImage = [[UIImage imageNamed:@'mask.png'] CGImage]; CGContextClipToMask(mainViewContentContext, CGRectMake(0, 0, targetSize.width, targetSize.height), maskImage); CGContextDrawImage(mainViewContentContext, CGRectMake(thumbnailPoint.x, thumbnailPoint.y, scaledWidth, scaledHeight), self.CGImage);   // Create CGImageRef of the main view bitmap content, and then // release that bitmap context CGImageRef mainViewContentBitmapContext = CGBitmapContextCreateImage(mainViewContentContext); CGContextRelease(mainViewContentContext);  // convert the finished resized image to a UIImage  UIImage *theImage = [UIImage imageWithCGImage:mainViewContentBitmapContext]; // image is retained by the property setting above, so we can  // release the original CGImageRelease(mainViewContentBitmapContext);  // return the image return theImage; 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 124k
  • Answers 124k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Are you running on OS 3.0? I saw the same… May 12, 2026 at 1:19 am
  • Editorial Team
    Editorial Team added an answer It looks like you need to register Apache::Session::Memcached with Apache::Session::Wrapper,… May 12, 2026 at 1:19 am
  • Editorial Team
    Editorial Team added an answer Use DATENAME or DATEPART: SELECT DATENAME(dw,GETDATE()) -- Friday SELECT DATEPART(dw,GETDATE())… May 12, 2026 at 1:19 am

Related Questions

I have a major headache trying to get an image that is contained within
I have an existing web app that allows users to rate items based on
I try to get to a page straight from Bash at http://www.ocwconsortium.org/ . The
I try to get some data about a city using Sparql query on DBpedia.

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.