【问题标题】:How to select a region of an UIImage and then create a new one out of it?如何选择 UIImage 的一个区域,然后从中创建一个新区域?
【发布时间】:2013-04-04 13:21:21
【问题描述】:

是否有一种简单的方法可以选择 UIImage 实例的区域,然后从中创建新图像?我尝试使用平移手势识别器来获取用户选择的坐标。但是有了这个选项,现在有可见的反馈,我还必须转换坐标。是否有一个不错的插件或最佳实践可以做到这一点?

【问题讨论】:

    标签: ios cocoa-touch cocoa core-graphics


    【解决方案1】:

    试试这个:

       UIGraphicsBeginImageContext(self.view.bounds.size);
    // retrieve the current graphics context
       CGContextRef context = UIGraphicsGetCurrentContext();
    // render view into context
       [self.view.layer renderInContext:context];
    // create image from context
       UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
       image=[self cropImage:image];
       UIGraphicsEndImageContext();
    
       - (UIImage *)cropImage:(UIImage *)oldImage {
           CGSize imageSize = oldImage.size;
           UIGraphicsBeginImageContextWithOptions(CGSizeMake( imageSize.width,imageSize.height - 150),NO,0.); //set your height and width
              [oldImage drawAtPoint:CGPointMake( 0, -80) blendMode:kCGBlendModeCopy alpha:1.];
              UIImage *croppedImage = UIGraphicsGetImageFromCurrentImageContext();
              UIGraphicsEndImageContext();
              return croppedImage;
       }
    

    【讨论】:

      【解决方案2】:

      像这样:

      CGImageRef imageRef = CGImageCreateWithImageInRect([bigImage CGImage], cropRect);
      UIImage *croppedImage = [UIImage imageWithCGImage:imageRef]; 
      CGImageRelease(imageRef);
      

      【讨论】:

        猜你喜欢
        • 2012-04-01
        • 1970-01-01
        • 2017-06-19
        • 1970-01-01
        • 2018-08-03
        • 1970-01-01
        • 2012-08-05
        • 1970-01-01
        • 2023-03-09
        相关资源
        最近更新 更多