问题
I have a mask and an image on which mask is applied to get a portion of that image. The problem is when I apply that mask on the image ,the resultant image from masking is of same size as the original image .Though the unmasked part is transparent. What I need is an image which only has the masked part of the original image ,I dont want transparent part to be in the image. so that the resultant image will be of smaller size an contain only the masked part.
Thanks
回答1:
You can:
- Draw the image to a new
CGBitmapContext
at actual size, providing a buffer for the bitmap.CGBitmapContextCreate
- Read alpha values from the bitmap to determine the transparent boundaries. You will have to determine how to read this based on the pixel data you have specified.
- Create a new
CGBitmapContext
providing the external buffer, using some variation or combination of: a) a pixel offset, b) offset bytes per row, or c) manually move the bitmap's data (in place to reduce memory usage, if possible).CGBitmapContextCreate
- Create a
CGImage
from the second bitmap context.CGBitmapContextCreateImage
来源:https://stackoverflow.com/questions/8834732/create-a-new-image-with-only-masked-part-without-transparent-area-with-new-siz