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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T23:09:29+00:00 2026-06-10T23:09:29+00:00

How can i extract an UIImage with an path? I need to get the

  • 0

How can i extract an UIImage with an path?

I need to get the content that’s inside the path into a new UIImage. What I need is the content of a rotated rectangle. This is the code that I use to get the corners of the rectangle. (x,y = center of image. width, height of image).

UIBezierPath* aPath = [UIBezierPath bezierPath];

//1
[aPath moveToPoint:CGPointMake(
                               x+(width/2)*cosf(A)-(height/2)*sinf(A),
                               y+(height/2)*cosf(A)+(width/2)*sinf(A))];

NSLog(@"%f, %f", x+(width/2)*cosf(A)-(height/2)*sinf(A),
      y+(height/2)*cosf(A)+(width/2)*sinf(A));


//2
[aPath moveToPoint:CGPointMake(
                               x-(width/2)*cosf(A)-(height/2)*sinf(A),
                               y+(height/2)*cosf(A)-(width/2)*sinf(A))];

NSLog(@"%f, %f", x-(width/2)*cosf(A)-(height/2)*sinf(A),
      y+(height/2)*cosf(A)-(width/2)*sinf(A));


//3
[aPath moveToPoint:CGPointMake(
                               x-(width/2)*cosf(A)+(height/2)*sinf(A),
                               y-(height/2)*cosf(A)-(width/2)*sinf(A))];

NSLog(@"%f, %f", x-(width/2)*cosf(A)+(height/2)*sinf(A),
      y-(height/2)*cosf(A)-(width/2)*sinf(A));


//4
[aPath moveToPoint:CGPointMake(
                               x+(width/2)*cosf(A)+(height/2)*sinf(A),
                               y-(height/2)*cosf(A)+(width/2)*sinf(A))];

NSLog(@"%f, %f", x+(width/2)*cosf(A)+(height/2)*sinf(A),
      y-(height/2)*cosf(A)+(width/2)*sinf(A));



//5
[aPath moveToPoint:CGPointMake(
                               x+(width/2)*cosf(A)-(height/2)*sinf(A),
                               y+(height/2)*cosf(A)+(width/2)*sinf(A))];

NSLog(@"%f, %f", x+(width/2)*cosf(A)-(height/2)*sinf(A),
      y+(height/2)*cosf(A)+(width/2)*sinf(A));
[aPath closePath];

I was thinking something like this: A picture of the problem.
(The shape is different here.) I want that yellow part to be a new UIImage.

  • 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-10T23:09:31+00:00Added an answer on June 10, 2026 at 11:09 pm

    Use your UIBezierPath as a clipping path. There is the addClip method for that.

    See the Quartz2D Programming Guide, especially this part for details.

    The idea is to create a new Bitmap context, apply the clipping using your path, then draw the image (that will thus get clipped) on this bitmap context and finally generate an UIImage from it.

    Moreover, instead of making some maths by yourself to rotate your CGRect you want to use for clipping, you should create a CGRect without any rotation, and use CGAffineTransform to rotate it. This will avoid the need to do the computation by yourself using cos/sin functions and make your code easier to read.


    [EDIT] Here is a full example to:

    • Generate a Bezier path and apply CGAffineTransform rotation to it
    • Use this bezier path to clip an image

    I did check it and works like a charm.

    // Generate a UIBezierPath of a rounded rect rotated by angle radians
    -(UIBezierPath*)computePathWithRect:(CGRect)rect
                           cornerRadius:(CGFloat)cornerRadius
                                  angle:(CGFloat)radians;
    {
      // Compute basic path
      UIBezierPath* path = [UIBezierPath bezierPathWithRoundedRect:rect cornerRadius:cornerRadius];
    
      // Apply rotation.
      // Don't forget that rotations are around origin 0,0 so if you want to rotate around the rect's center,
      // you need to apply a translation so that the rect's center is at 0,0, then rotate, and translate it back at its original position.
      CGAffineTransform restoreCenterPosition = CGAffineTransformMakeTranslation(CGRectGetMidX(rect), CGRectGetMidY(rect));
      CGAffineTransform rotateFromOrigin = CGAffineTransformRotate(restoreCenterPosition,radians);
      CGAffineTransform rotateFromRectCenter = CGAffineTransformTranslate(rotateFromOrigin, -CGRectGetMidX(rect), -CGRectGetMidY(rect));
      [path applyTransform:rotateFromRectCenter];
    
      return path;
    }
    
    // Generate a new image from srcImage but clipped with the given path
    -(UIImage*)clipImage:(UIImage*)srcImage withPath:(UIBezierPath*)path
    {
      UIGraphicsBeginImageContextWithOptions(srcImage.size, NO, 0.0);
    
      // Add clipping path
      // Actually UIBezierPath has a method for that, that is equivalent to CGContextClip(currentContext, bezierPath.CGPath), so better use it
      [path addClip];
    
      // Flip coordinates before drawing image as UIKit and CoreGraphics have inverted coordinate system
      CGContextTranslateCTM(UIGraphicsGetCurrentContext(), 0, srcImage.size.height);
      CGContextScaleCTM(UIGraphicsGetCurrentContext(), 1, -1);
      // Draw image, that will thus be clipped, on the bitmap context
      CGContextDrawImage(UIGraphicsGetCurrentContext(), CGRectMake(0, 0, srcImage.size.width, srcImage.size.height), srcImage.CGImage);
    
      // Generate final (clipped) image
      UIImage* clippedImage = UIGraphicsGetImageFromCurrentImageContext();
      UIGraphicsEndImageContext();
    
      return clippedImage;
    }
    
    
    // Usage example
    - (void)doClipping
    {
      UIImage* originalImage = ...
      UIBezierPath* path = [self computePathWithRect:CGRectMake(100,100,184,94)
                                        cornerRadius:10.f
                                               angle:30*M_PI/180.f];
      UIImage* clippedImage = [self clipImage:originalImage withPath:path];
      self.resultImageView.image = clippedImage;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need a piece of code with which I can extract the substrings that
I need a library that can extract text from handwritten paper once I scan
I need to transform the incoming XML so that I can extract all item
I'm looking for a parser that can extract methods from a java class (static
To have a general-purpose documentation system that can extract inline documentation of multiple languages,
I have some path c:\server\folderName1\another name\something\another folder\ . How i can extract from there
How can we extract a line from a multiLine EditText ? I tried this
Does anyone know of any software that can extract the $bitmap file from NTFS
The quick question: is there a pure PHP library that can extract a frame
i'm looking for an alternative to wyd 0.2 that can extract strings from any

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.