我正在开发一个 iPhone 应用程序,我可以用相机拍照。此后应该可以将图像切片为各种形状 - 最重要的是三角形。任何人都可以给我一个正确方向的观点或示例等。我已经可以将其切成正方形,但不能切成三角形。
(更新)为了创建方形切片,我使用了以下代码片段
CGRect ClippedRect= CGRectMake(0, 150, 320.0, 230.0);//example numbers
CGImageRef imageRef = CGImageCreateWithImageInRect([OriginalUIImage CGImage], ClippedRect);
UIImage *resultUIImage=[[UIImage alloc]initWithCGImage:imageRef];
所有帮助
感谢
解决了:
我采用了屏蔽 uiimage 的方法。所以策略是 1. 用相机和秤拍照。 2. 让用户在 uiimageview 中绘制一个图形并用黑色填充它,然后从中创建一个 UIImage。 3. 使用用户生成的图像遮盖来自相机的图像。
为了屏蔽图像,我使用 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;
}
感谢您的所有帮助
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
发布评论
评论(2)
尝试这样的事情:
http://iosdevelopertips.com/cocoa/how-to-mask- an-image.html
如果我的解释正确的话,只需制作一个黑色图像作为蒙版并将其应用到图像上即可。
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.
您应该能够使用这种方法:使用 UIGraphicsBeginImageContextWithOptions (您也可以使用 CoreGraphics 等效项,但需要更多工作),然后创建所需形状的贝塞尔曲线路径,调用 [yourBezierPath addClip],然后在该上下文中绘制图像。然后,您可以使用 UIGraphicsGetImageFromCurrentImageContext。请务必随后调用
UIGraphicsEndImageContext()
。有关使用UIGraphicsBeginImageContextWithOptions
创建图像的信息,请参阅此问题 。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 withUIGraphicsBeginImageContextWithOptions
.