0

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

user1085093
  • 507
  • 1
  • 5
  • 9
  • Can you explain with some screen shot or something? What i am getting is that u r trying to apply cropping or something. – Yama Jan 12 '12 at 12:08
  • @Sarah .what I am trying to do is similar to crop ,but with irregular shape ,since masked shape will be irregular. I have a background image (image1)(size:400x400) and a mask(irregular shape -jigsaw)) . When I apply mask on background-image -what I want is an image of shape of jigsaw only .I dont want transparent background which we get on application of mask .what I am getting now after applying masking is an image with size same as original image(400x400) with transparent background . I want to save that masked part in a new image (with its own dimensions ) – user1085093 Jan 12 '12 at 12:15

1 Answers1

0

You can:

  1. Draw the image to a new CGBitmapContext at actual size, providing a buffer for the bitmap. CGBitmapContextCreate
  2. 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.
  3. 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
  4. Create a CGImage from the second bitmap context. CGBitmapContextCreateImage
justin
  • 104,054
  • 14
  • 179
  • 226
  • Actually new to iphone dev. I used this article to mask my image: [link](http://iphonedevelopertips.com/cocoa/how-to-mask-an-image.html) .Can u suggest ur changes using some code or changes that I should make to the code specified in this tutorial – user1085093 Jan 12 '12 at 12:34
  • I've updated the answer with the CG calls you will need. Dealing with bitmap data is pretty universal, once you know how its layout can vary. Unfortunately, a full sample/explanation is somewhat long. Wait - maybe somebody else will answer with that level of detail. – justin Jan 12 '12 at 12:42
  • thanks fr your answer. Actually now I have made some modifications in my code .SO I no longer need irregular shaped masked area .Now even rectangular shaped cropped image will suffice . Is there any easier way using which I can CROP masked area from the original image – user1085093 Jan 13 '12 at 09:20
  • the approach would be the same. what i outlined implies a cropped rectangle. easier way? an implementation likely exists in the open domain, but there's a good chance that this is how it too would be implemented. – justin Jan 16 '12 at 05:26