我需要以編程方式將圖像分成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