简体   繁体   中英

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.

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))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM