【问题标题】:select part of image (clipping)选择图像的一部分(剪辑)
【发布时间】:2011-01-15 00:13:12
【问题描述】:

这可能没有我想象的那么难,但是我如何选择图像的一部分呢?考虑以下示例:

灰色区域是PNG或JPG格式的图像,现在我想从中选择红色的80x80 px区域。应显示红色区域,其余不显示。我尝试了很多方法:

  • 将 UIImageView 的 clipsToBounds 设置为 YES 并尝试偏移图像(不起作用)
  • 剪辑上视图(不起作用)

我还查看了绘图功能,但我还没有使用这些功能,这对我来说似乎有点牵强。有什么明显的方法可以做我错过的事情吗?我不希望图像被调整大小,只是被剪裁了。

感谢您的宝贵时间!

【问题讨论】:

  • +1 可以很好地解释您的问题。

标签: iphone objective-c cocoa-touch uiimageview uiimage


【解决方案1】:

this问题中窃取以下代码并寻找HitScan的答案。

CGImageRef imageRef = CGImageCreateWithImageInRect([largeImage CGImage], cropRect);
// or use the UIImage wherever you like
[UIImageView setImage:[UIImage imageWithCGImage:imageRef]]; 
CGImageRelease(imageRef);

【讨论】:

    【解决方案2】:

    如果您只想显示图像的中心 UIImageView 通过 IB 将内容模式设置为“Aspect Fill”。这将使它居中并裁剪掉任何多余的东西。

    或者在代码中

    myImageView.contentMode = UIViewContentModeScaleAspectFill;
    

    【讨论】:

      【解决方案3】:
      myImageView.contentMode = UIViewContentModeCenter;
      myImageView.clipsToBounds = YES;
      

      【讨论】:

        【解决方案4】:

        请参阅下面的代码。在这里,我提供了一个 imageview 并将其拆分为两个相等的 imageview。

        - (void)splitImageView:(UIImageView*)imgView{
        
        UIImage *image = imgView.image;
        CGRect rectForDrawing = CGRectMake(0, 0, imgView.frame.size.width/2, imgView.frame.size.height);
        
        CGRect leftRect = CGRectMake(0, 0, imgView.frame.size.width/2, imgView.frame.size.height);
        CGRect rightRect = CGRectMake(imgView.frame.size.width/2, 0, imgView.frame.size.width/2, imgView.frame.size.height);
        
        
        CGImageRef leftReference =  CGImageCreateWithImageInRect([image CGImage], leftRect);
        CGImageRef rightReference = CGImageCreateWithImageInRect([image CGImage], rightRect);
        
        UIGraphicsBeginImageContextWithOptions(rectForDrawing.size, NO, 0);
            CGContextRef con = UIGraphicsGetCurrentContext();
            CGContextDrawImage(con, rectForDrawing,flip(leftReference));
            imgViewLeft = [[UIImageView alloc] initWithImage:UIGraphicsGetImageFromCurrentImageContext()];
            imgViewLeft.frame = CGRectMake(imgView.frame.origin.x, imgView.frame.origin.y,
                                       imgView.frame.size.width/2, imgView.frame.size.height);
        
            CGContextDrawImage(con, rectForDrawing,flip(rightReference));
            imgViewRight = [[UIImageView alloc] initWithImage:UIGraphicsGetImageFromCurrentImageContext()];
            imgViewRight.frame = CGRectMake(imgView.frame.origin.x+imgView.frame.size.width/2, imgView.frame.origin.y,
                                        imgView.frame.size.width/2, imgView.frame.size.height);
        
        
        UIGraphicsEndImageContext();
        
        
        [self.view addSubview:imgViewLeft];
        [self.view addSubview:imgViewRight]; 
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2020-09-30
          • 1970-01-01
          • 2015-09-24
          • 2012-10-28
          • 2021-09-03
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多