【发布时间】:2011-07-19 17:29:54
【问题描述】:
我正在开发一个用相机拍照的 iPhone 应用程序。此后应该可以将图像切成各种形状 - 最重要的三角形。谁能给我一个正确方向的观点或示例等。我已经可以切成正方形而不是三角形。
(已更新)为了创建平方切片,我使用了以下代码 sn-ps
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;
}
感谢大家的帮助
【问题讨论】:
-
你用什么把它们滑成正方形?
-
这里有很多关于切片成正方形而不是三角形的好帖子。
-
我只是想问一下你用的是哪一个正方形。我自己在这方面没有任何经验,所以我需要一个起点来提供帮助。此外,如果您提供此类其他详细信息,对其他人将更加有用。
-
确定我已经更新了信息 - 感谢所有帮助
标签: iphone uiimage geometry slice image-clipping