iphoneuiimagegeometrysliceimage-clipping

Slicing UIImage into triangles in iPhone app


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


Solution

  • 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.