iphone - 如何使用矩形裁剪图像?

标签 iphone ios image-processing uiimageview uiimage

Rectangle used to crop

我正在尝试使用此矩形裁剪图像。但不知道如何拉伸(stretch)矩形。我有一个 UIView ,它位于显示有 黑色边框的另一个 View 的外部和内部。当我拉伸(stretch) DO 时,我需要更改两个 UIView 的框架??

应该使用Pinchgesture来拉伸(stretch)矩形还是任何其他方法?

这里四个蓝色处理程序是 UIView 。我将如何确定它们的拖动? 如果有请推荐。

最佳答案

以下代码用于选择框并裁剪图像。

-(IBAction) cropImage:(id) sender{
    // Create rectangle that represents a cropped image  
    // from the middle of the existing image
float xCo,yCo;
float width=bottomCornerPoint.x-topCornerPoint.x;
float height=bottomCornerPoint.y-topCornerPoint.y;
if(width<0)
width=-width;
if(height<0)
    height=-height;
         if(topCornerPoint.x <bottomCornerPoint.x)
         {
               xCo=topCornerPoint.x;
          }
         else 
         {
             xCo=bottomCornerPoint.x;
         } 
        if(topCornerPoint.y <bottomCornerPoint.y)
        {
             yCo=topCornerPoint.y;
         }
      else {
             yCo=bottomCornerPoint.y;
       }
    CGRect rect = CGRectMake(xCo,yCo,width,height);
    // Create bitmap image from original image data,
    // using rectangle to specify desired crop area
    UIImage *image = [UIImage imageNamed:@"abc.png"];
    CGImageRef imageRef = CGImageCreateWithImageInRect([image CGImage], rect);
    UIImage *img = [UIImage imageWithCGImage:imageRef]; 
    CGImageRelease(imageRef);
    // Create and show the new image from bitmap data
    imageView = [[UIImageView alloc] initWithImage:img];
    [imageView setFrame:CGRectMake(110, 600, width, height)];
    imageView.image=img;
    [[self view] addSubview:imageView];
    [imageView release];
}

希望这可以帮助很多

关于iphone - 如何使用矩形裁剪图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12004419/

相关文章:

C# - 如何从图像中删除除黑色以外的所有颜色

python - 拉普拉斯金字塔后的重建图像与原始图像不一样

python - 从裁剪区域中提取像素数

iphone - 在 iPad 应用程序中从 UIView 创建 pdf 的问题

java - 移动企业应用平台(MEAP)有推荐吗?

iphone - 核心数据获取不一致

ios - 创建另一个新节点时,SKShapeNode 中的节点重新出现

ios - 设置内容插入后,iOS7 上的 UIWebView 中的部分黑色背景

ios - 如何显示一个窗口?

ios - UILabel 中的自动换行文本,末尾附加 "..."