【问题标题】:how to crop image in to pieces programmatically如何以编程方式将图像裁剪成碎片
【发布时间】:2011-01-20 03:44:48
【问题描述】:

我需要以编程方式将图像分成 9 块。关于如何做到这一点的任何建议?

【问题讨论】:

    标签: iphone core-graphics


    【解决方案1】:

    下面的代码也是一种检测被点击的图片的解决方案。这个想法是获取 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);
    }
    

    【讨论】:

    • 你说得对,我怎样才能让这些图片动起来
    • UIView 的位置由 .frame 和 .center 属性设置。 Core Animation 可能也值得一看。
    • UIView 看起来像静态的,我该如何更改它们是可移动的。
    • @MaheshBabu:你应该问一个关于这个的新问题。
    • @Evan:我认为您不必将图像视图放在视图中即可检测手势。但是你必须启用UserInteraction。因为这是在图像视图中设置为 NO 的标准。 ;-)
    【解决方案2】:

    有很多方法可以对图像进行切片和切块,但这里有一种。它使用 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 个图像的数组,每个图像的大小(with/3, height/3)

    【讨论】:

    • 这将从下到上获取图像,因为对于 CGImage,(0,0) 是左下而不是左上。因此,要从左上角获取图像,请将代码修改为 CGRect portion = CGRectMake(i * size.width/3.0, (2-j) * size.height/3.0, size.width/3.0, size.height/3.0);
    • 很好地抓住了 nimeshdesai。既然您提到了,我想我用对称图像对此进行了测试。我将进行快速测试并进行相应的修改。干杯!
    • 随时。我为一个项目测试了你的代码并发现了一个错误,我想会让你知道:)
    【解决方案3】:

    以下代码切片图片基于参数,添加边框和显示:

    -(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];

    【讨论】:

    • 如果我需要破坏该图像然后进一步更改图像怎么办??
    猜你喜欢
    • 2012-02-28
    • 1970-01-01
    • 1970-01-01
    • 2013-03-22
    • 2017-02-19
    • 2011-01-08
    • 1970-01-01
    • 2014-04-02
    • 1970-01-01
    相关资源
    最近更新 更多