我需要以编程方式将图像分成9个部分。有关如何做到这一点的任何建议?如何以编程方式将图像裁剪为零件
8
A
回答
13
下面的代码也是一个解决方案,它可以检测到被点击的图片。这个想法是采取UIImage,并使用CGImageCreateWithImageInRect裁剪件。从裁剪的部分创建一个新的UIImage并将其放置在UIImageView中。为了获得点击手势,我必须将UIImageView放置在UIView中。最后,提供手势和一个独特的标签,以便点击时可以识别该作品。
- (void)loadView {
UIView* root = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]];
UIImage* whole = [UIImage imageNamed:@"whole.jpg"]; //I know this image is 300x300
int partId = 0;
for (int x=0; x<=200; x+=100) {
for(int y=0; y<=200; y+=100) {
CGImageRef cgImg = CGImageCreateWithImageInRect(whole.CGImage, CGRectMake(x, y, 100, 100));
UIImage* part = [UIImage imageWithCGImage:cgImg];
UIImageView* iv = [[UIImageView alloc] initWithImage:part];
UIView* sView = [[UIView alloc] initWithFrame:CGRectMake(200-x, 200-y, 100, 100)];
[sView addSubview:iv];
[iv release];
UITapGestureRecognizer* tap = [[UITapGestureRecognizer alloc] initWithTarget:self
action:@selector(tap:)];
tap.numberOfTapsRequired = 1;
[sView addGestureRecognizer:tap];
[tap release];
sView.tag = partId;
[root addSubview:sView];
[sView release];
partId++;
CGImageRelease(cgImg);
}
}
self.view = root;
}
- (void)tap:(UITapGestureRecognizer*)gesture
{
NSLog(@"image tap=%d", gesture.view.tag);
}
6
有很多方法来切片和切块图像,但这里是一个。它使用Quartz将图像剪切成9个相同大小的分数。请注意,它不处理旋转图像(!我的意思是与imageOrientation图像= 0),但它应该让你开始:
+(NSArray *)splitImageInTo9:(UIImage *)im{
CGSize size = [im size];
NSMutableArray *arr = [[NSMutableArray alloc] initWithCapacity:9];
for (int i=0;i<3;i++){
for (int j=0;j<3;j++){
CGRect portion = CGRectMake(i * size.width/3.0, j * size.height/3.0, size.width/3.0, size.height/3.0);
UIGraphicsBeginImageContext(portion.size);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextScaleCTM(context, 1.0, -1.0);
CGContextTranslateCTM(context, 0, -portion.size.height);
CGContextTranslateCTM(context, -portion.origin.x, -portion.origin.y);
CGContextDrawImage(context,CGRectMake(0.0, 0.0,size.width, size.height), im.CGImage);
[arr addObject:UIGraphicsGetImageFromCurrentImageContext()];
UIGraphicsEndImageContext();
}
}
return [arr autorelease];
}
的输出将是9个图像阵列,每个尺寸(有/ 3 ,height/3)
0
如果你想在图像的部分将被组织,你需要使用一些UIImageView的最终观点..只是来看看下面的代码:基于
UIImage* whole = [UIImage imageNamed:@"permanent_cosmetics_pretty.jpg"];
int partId = 0;
for (int x=0; x<=300; x+=100) {
for(int y=0; y<=300; y+=100) {
CGImageRef cgImg = CGImageCreateWithImageInRect(whole.CGImage, CGRectMake(x, y, 100, 100));
UIImage* part = [UIImage imageWithCGImage:cgImg];
UIImageView* iv = [[UIImageView alloc] initWithImage:part];
switch (partId) {
case 0:
self.part1.image=iv.image;
break;
case 1:
self.part2.image=iv.image;
break;
case 2:
self.part3.image=iv.image;
break;
case 3:
self.part4.image=iv.image;
break;
case 4:
self.part5.image=iv.image;
break;
case 5:
self.part6.image=iv.image;
break;
case 6:
self.part7.image=iv.image;
break;
case 7:
self.part8.image=iv.image;
break;
case 8:
self.part9.image=iv.image;
break;
case 9:
self.part10.image=iv.image;
break;
case 10:
self.part11.image=iv.image;
break;
case 11:
self.part12.image=iv.image;
break;
default:
break;
}
[iv release];
partId++;
NSLog(@"part id = %d",partId);
}
}
[self.view addSubview:self.finalView];
1
下面的代码片段图像参数上,添加边框和显示器:
-(NSMutableArray *)getImagesFromImage:(UIImage *)image withRow:(NSInteger)rows withColumn:(NSInteger)columns{
NSMutableArray *images = [NSMutableArray array];
CGSize imageSize = image.size;
CGFloat xPos = 0.0, yPos = 0.0;
CGFloat width = imageSize.width/rows;
CGFloat height = imageSize.height/columns;
for (int y = 0; y < columns; y++) {
xPos = 0.0;
for (int x = 0; x < rows; x++) {
CGRect rect = CGRectMake(xPos, yPos, width, height);
CGImageRef cImage = CGImageCreateWithImageInRect([image CGImage], rect);
UIImage *dImage = [[UIImage alloc] initWithCGImage:cImage];
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(x*width, y*height, width, height)];
[imageView setImage:dImage];
[imageView.layer setBorderColor:[[UIColor blackColor] CGColor]];
[imageView.layer setBorderWidth:1.0];
[self.view addSubview:imageView];
[images addObject:dImage];
xPos += width;
}
yPos += height;
}
return images;
}
项目下载链接:https://github.com/bpolat/Image-Slicer
样品的使用和结果:
[self getImagesFromImage:[UIImage imageNamed:@“1.png”] withRow:4 withColumn:4];
相关问题
- 1. 以编程方式将图像裁剪为小矩形
- 2. iPhone以编程方式裁剪方形图像以显示为圆形
- 3. 以编程方式从底部裁剪图像
- 4. 将图像裁剪为正方形 - Android
- 5. 以编程方式裁剪位图的底部部分
- 6. 如何使用CSS将矩形图像裁剪为正方形?
- 7. 如何裁剪图像?
- 8. 如何将图像裁剪成圆形?
- 9. 如何将图像裁剪为最大图像矩形?
- 10. 裁剪图像
- 11. 裁剪图像
- 12. 使用Javascript以编程方式剪切/剪切图像
- 13. 将图像裁剪为弹出?
- 14. 图像裁剪组件
- 15. 如何以编程方式将图像设置为壁纸?
- 16. 无法剪裁/裁剪图像
- 17. 以任何形状裁剪图像
- 18. 如何通过编程在钛中裁剪图像?
- 19. 如何在裁剪之前裁剪大尺寸图像而不裁剪?
- 20. WPF图像裁剪
- 21. html5图像裁剪
- 22. WPF图像裁剪
- 23. 笨裁剪图像
- 24. 裁剪android图像
- 25. GWT图像裁剪
- 26. Silverlight图像裁剪
- 27. 裁剪YUV图像
- 28. raphael.js裁剪图像
- 29. iOS - 以编程方式移动/裁剪UIView
- 30. 使用meio上传以编程方式裁剪照片
这将让图像从底部以来的CGImage到顶部,(0,0)是左下角,而不是左上角。 因此,要从左上角获取图像,请修改您的代码以读取 `CGRect部分= CGRectMake(i * size.width/3.0,(2-j)* size.height/3.0,size.width/3.0,size .height/3.0);` – nimeshdesai 2012-02-17 05:37:07
不错的捕捉nimeshdesai。现在你提到我认为我用对称图像测试了这个。我将运行一个快速测试并进行相应的修改。干杯! – fsaint 2012-02-17 07:12:01
随时。我测试了一个项目的代码,发现了一个bug,想过会让你知道:) – nimeshdesai 2012-02-17 19:27:46