1

I am working on a iPhone app where I take a picture with the camera. Hereafter it should be possible to slice the image in various shapes - most important triangles. Can anyone give me a point in the right direction or examples etc. I have made it possible to slice into squares but not triangles.

(Updated)For creating the squared slices I used the following code snippets

CGRect ClippedRect= CGRectMake(0, 150, 320.0, 230.0);//example numbers
CGImageRef imageRef = CGImageCreateWithImageInRect([OriginalUIImage CGImage], ClippedRect);
UIImage *resultUIImage=[[UIImage alloc]initWithCGImage:imageRef];

All help is appreciated

Regards

SOLVED:

I went with the method of masking the uiimage. So the strategy is that 1. Take picture from camera and scale. 2. Let the user draw a figure in a uiimageview and fill it with black color and the create a UIImage from this. 3. Mask the image from the camera with the user generated image. For masking the image I use the following method from http://www.developers-life.com/resize-and-mask-an-image.html

- (UIImage*) maskImage:(UIImage *)image  {

 CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();

 UIImage *maskImage = [UIImage imageNamed:@"mask.png"];
 CGImageRef maskImageRef = [maskImage CGImage];

 // create a bitmap graphics context the size of the image
 CGContextRef mainViewContentContext = CGBitmapContextCreate (NULL, maskImage.size.width, maskImage.size.height, 8, 0, colorSpace, kCGImageAlphaPremultipliedLast);


 if (mainViewContentContext==NULL)
      return NULL;

 CGFloat ratio = 0;

 ratio = maskImage.size.width/ image.size.width;

 if(ratio * image.size.height < maskImage.size.height) {
      ratio = maskImage.size.height/ image.size.height;
 } 

 CGRect rect1  = {{0, 0}, {maskImage.size.width, maskImage.size.height}};
 CGRect rect2  = {{-((image.size.width*ratio)-maskImage.size.width)/2 , -((image.size.height*ratio)-maskImage.size.height)/2}, {image.size.width*ratio, image.size.height*ratio}};


 CGContextClipToMask(mainViewContentContext, rect1, maskImageRef);
 CGContextDrawImage(mainViewContentContext, rect2, image.CGImage);


 // Create CGImageRef of the main view bitmap content, and then
 // release that bitmap context
 CGImageRef newImage = CGBitmapContextCreateImage(mainViewContentContext);
 CGContextRelease(mainViewContentContext);

 UIImage *theImage = [UIImage imageWithCGImage:newImage];

 CGImageRelease(newImage);

 // return the image
 return theImage;

}

Thank you for all the help

user1118321
  • 25,567
  • 4
  • 55
  • 86
Bjarke
  • 1,283
  • 11
  • 36
  • What are you using to slide them in squares? – Erik S Mar 21 '11 at 14:21
  • There is a lot of good post in here about slicing into squares but not triangles. – Bjarke Mar 21 '11 at 14:30
  • I just meant to ask which one you have been using for squares. I myself don't have any experience in this, so i would need a starting point to help. Also, if you provide with additional details like this, it will be a lot more useful to others. – Erik S Mar 21 '11 at 14:34
  • Sure I have updated the info - all help is appreciated – Bjarke Mar 21 '11 at 14:59

2 Answers2

1

Try something like this:
http://iosdevelopertips.com/cocoa/how-to-mask-an-image.html
Simply make a black image as your mask and apply it to the image, if i get this explanation right.

Cœur
  • 37,241
  • 25
  • 195
  • 267
Erik S
  • 1,939
  • 1
  • 18
  • 44
  • 1
    You need to create greyscale images for each mask and each image size. If input images are always of the same sizes and there's only a limited set of masks, then this solution surely is easier than using a clipping path. – DarkDust Mar 21 '11 at 15:24
  • Hm, didn't take size into consideration indeed. But seeing that he's taking them from the camera, i suppose they're always the same resolution; greyscale images can easily size up/down for higher resolution cameras i suppose. – Erik S Mar 21 '11 at 16:21
  • Not if you want sharp edges, they get blurry when scaling. And I'm not sure about the cameras resolution, AFAIK the resolution of the front facing cameras are lower on iDevices than the one on the back. But if he scales the photos all to the same sizes then the mask images will work alright. – DarkDust Mar 21 '11 at 18:11
  • Thank you - I will take a look at that - however the triangles are not the same size every time. So maybe I will have to look at a clipping path as @DarkDust are mentioning – Bjarke Mar 21 '11 at 18:23
1

You should be able to use this approach: Create a new image using UIGraphicsBeginImageContextWithOptions (you could also use the CoreGraphics equivalent but is much more work), then create a bezier path of the shape you want, call [yourBezierPath addClip] and then draw your image in that context. You can then get the resulting image using UIGraphicsGetImageFromCurrentImageContext. Be sure to call UIGraphicsEndImageContext() afterwards. See this question for creating an image with UIGraphicsBeginImageContextWithOptions.

Community
  • 1
  • 1
DarkDust
  • 90,870
  • 19
  • 190
  • 224
  • This looks as a rather good suggestion - I will give it a try tomorrow and report back how it went – Bjarke Mar 21 '11 at 20:36