Arbitur
Arbitur

Reputation: 39091

UIImage drawInRect

I want to "extract" a apart of a UIImage but Im having some troubles.

For example the original imagesize is 320x480 and I want to get render a new UIImage in a rect like CGRect(10, 10, 100, 100)

Ive not found a good solution but Ive found something that might be close to the right solution: drawInrect().

But when I use that everything else but within that rect gets black.

Please help me.

Upvotes: 1

Views: 2823

Answers (1)

K Bakalov
K Bakalov

Reputation: 284

If what you are trying to achieve is a cropped version of the original image, then you should know that the canvas you are drawing in as frame with {0,0} origin. So for example if your desired effect is to get an image with size 100x100 and origin {10, 10}. So ..

let imageSize = image.size
UIGraphicsBeginImageContextWithOptions(CGSizeMake(100.0, 100.0), true, 0.0)
image.drawInRect(CGRectMake(-10, -10, imageSize.width, imageSize.height))

Upvotes: 3

Related Questions