Eva*_*van 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);
}
Run Code Online (Sandbox Code Playgroud)
有很多方法可以对图像进行切片和切块,但这里有一个.它使用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];
}
Run Code Online (Sandbox Code Playgroud)
输出将是9个图像的数组,每个图像的大小(/ 3,高度/ 3)
| 归档时间: |
|
| 查看次数: |
11103 次 |
| 最近记录: |