【问题标题】:How to crop Image using rectangle?如何使用矩形裁剪图像?
【发布时间】:2012-08-13 19:42:25
【问题描述】:

我正在尝试使用此矩形裁剪图像。但不知道如何拉伸矩形。我有一个UIView,它在black border 显示的另一个视图的外部和内部。当我拉伸时,我需要更改 UIViews 的框架吗??

应该使用Pinchgesture拉伸矩形还是其他方法??

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

【问题讨论】:

标签: iphone ios image-processing uiimageview uiimage


【解决方案1】:

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

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

希望对大家有帮助

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多