【问题标题】:Split UIImage in half?将 UIImage 分成两半?
【发布时间】:2012-05-26 12:56:32
【问题描述】:

我将如何将 UIImage 分成两半(从中间向下)以便生成两个图像?

【问题讨论】:

    标签: ios xcode cocoa-touch ipad xcode4.3


    【解决方案1】:

    你可以试试这个,

    UIImage *image = [UIImage imageNamed:@"yourImage.png"];
    CGImageRef tmpImgRef = image.CGImage;
    CGImageRef topImgRef = CGImageCreateWithImageInRect(tmpImgRef, CGRectMake(0, 0, image.size.width, image.size.height / 2.0));
    UIImage *topImage = [UIImage imageWithCGImage:topImgRef];
    CGImageRelease(topImgRef);
    
    CGImageRef bottomImgRef = CGImageCreateWithImageInRect(tmpImgRef, CGRectMake(0, image.size.height / 2.0,  image.size.width, image.size.height / 2.0));
    UIImage *bottomImage = [UIImage imageWithCGImage:bottomImgRef];
    CGImageRelease(bottomImgRef);
    

    希望对你有帮助,:)

    【讨论】:

      【解决方案2】:
      - (void)splitImage:(UIImage *)image
      {
          CGFloat imgWidth = image.size.width/2;
          CGFloat imgheight = image.size.height;
      
          CGRect leftImgFrame = CGRectMake(0, 0, imgWidth, imgheight);
          CGRect rightImgFrame = CGRectMake(imgWidth, 0, imgWidth, imgheight);
      
          CGImageRef left = CGImageCreateWithImageInRect(image.CGImage, leftImgFrame);
          CGImageRef right = CGImageCreateWithImageInRect(image.CGImage, rightImgFrame);
      
          // These are the images we want!
          UIImage *leftImage = [UIImage imageWithCGImage:left];
          UIImage *rightImage = [UIImage imageWithCGImage:right];
      
          // Don't forget to free the memory!
          CGImageRelease(left);
          CGImageRelease(right);
      }
      

      【讨论】:

      • 这是唯一不会创建一大堆上下文的答案。 Quartz 在优化裁剪过程方面做得很好(直接获取图像的像素数据,而不是创建新的上下文,绘制图像,然后裁剪)。当我们将图像分成许多小部分时,这可能会快很多倍!
      【解决方案3】:

      iOS 11,swift 4.0 更新版本https://stackoverflow.com/users/893872/durul-dalkanat,老实说我认为是最好的:)

      这会将图像分成 4 个部分

      func splitImage(image2D: UIImage) -> [UIImage] {
          let imgWidth = image2D.size.width / 2
          let imgHeight = image2D.size.height / 2
          var imgImages:[UIImage] = []
      
          let leftHigh = CGRect(x: 0, y: 0, width: imgWidth, height: imgHeight)
          let rightHigh = CGRect(x: imgWidth, y: 0, width: imgHeight, height: imgHeight)
          let leftLow = CGRect(x: 0, y: imgHeight, width: imgWidth, height: imgHeight)
          let rightLow = CGRect(x: imgWidth, y: imgHeight, width: imgWidth, height: imgHeight)
      
          let leftQH = image2D.cgImage?.cropping(to:leftHigh)
          let rightHQ = image2D.cgImage?.cropping(to:rightHigh)
          let leftQL = image2D.cgImage?.cropping(to:leftLow)
          let rightQL = image2D.cgImage?.cropping(to:rightLow)
      
          imgImages.append(UIImage(cgImage: leftQH!))
          imgImages.append(UIImage(cgImage: rightHQ!))
          imgImages.append(UIImage(cgImage: leftQL!))
          imgImages.append(UIImage(cgImage: rightQL!))
      
          return imgImages
      }
      

      【讨论】:

        猜你喜欢
        • 2013-05-25
        • 2013-12-06
        • 1970-01-01
        • 2015-12-02
        • 2020-07-20
        • 2019-09-04
        • 1970-01-01
        • 1970-01-01
        • 2020-09-21
        相关资源
        最近更新 更多