2016-09-23 181 views
14

目标:裁剪UIImage如何在不丢失scale属性的情况下裁剪UIImage?

我执行以下码(与2.0的scale属性开始):

let croppedCGImage = originalUIImage.cgImage!.cropping(to: cropRect) 
let croppedUIImage = UIImage(cgImage: croppedCGImage!) 

此代码的工作,然而,结果是,croppedUIImage,具有一个不正确的scale属性1.0。


我试图创建最终的图像时指定scale

let croppedUIImage = UIImage(cgImage: croppedCGImage!, scale: 2.0, orientation: .up) 

这将产生正确的比例,但它切割size尺寸一半不正确。


我应该在这里做什么?

(*注:因为我后来保存UIImagePNGRepresentation(_ image: UIImage)这是由scale属性影响图像的scale财产上的UIImage很重要):



编辑以下工作。不幸的是,它的速度比CGImage裁剪功能慢得多。

extension UIImage { 
    func cropping(to rect: CGRect) -> UIImage { 
     UIGraphicsBeginImageContextWithOptions(rect.size, false, self.scale) 

     self.draw(in: CGRect(x: -rect.origin.x, y: -rect.origin.y, width: self.size.width, height: self.size.height)) 

     let croppedImage = UIGraphicsGetImageFromCurrentImageContext()! 
     UIGraphicsEndImageContext() 

     return croppedImage 
    } 
} 

回答

6

试试这个:

extension UIImage { 
    func imageByCropToRect(rect:CGRect, scale:Bool) -> UIImage { 

     var rect = rect 
     var scaleFactor: CGFloat = 1.0 
     if scale { 
      scaleFactor = self.scale 
      rect.origin.x *= scaleFactor 
      rect.origin.y *= scaleFactor 
      rect.size.width *= scaleFactor 
      rect.size.height *= scaleFactor 
     } 

     var image: UIImage? = nil; 
     if rect.size.width > 0 && rect.size.height > 0 { 
      let imageRef = self.cgImage!.cropping(to: rect) 
      image = UIImage(cgImage: imageRef!, scale: scaleFactor, orientation: self.imageOrientation) 
     } 

     return image! 
    } 
} 
4

使用此扩展: -

extension UIImage { 

    func cropping(to quality: CGInterpolationQuality, rect: CGRect) -> UIImage { 
     UIGraphicsBeginImageContextWithOptions(rect.size, false, self.scale) 

     let context = UIGraphicsGetCurrentContext()! as CGContext 
     context.interpolationQuality = quality 

     let drawRect : CGRect = CGRect(x: -rect.origin.x, y: -rect.origin.y, width: self.size.width, height: self.size.height) 

     context.clip(to: CGRect(x:0, y:0, width: rect.size.width, height: rect.size.height)) 

     self.draw(in: drawRect) 

     let croppedImage = UIGraphicsGetImageFromCurrentImageContext()! 
     UIGraphicsEndImageContext() 

     return croppedImage 
    } 
} 
+0

你怎么想到这个代码就会突然从矩形的位置和大小的图像?它不适合我 –

3

我使用的是ImageHelper波德iOS和tvOS,它的正常使用,也可能适合您的需求。

它带来了很多的UIImage扩展,如:

作物和调整

// Crops an image to a new rect 
func crop(bounds: CGRect) -> UIImage? 

// Crops an image to a centered square 
func cropToSquare() -> UIImage? { 

// Resizes an image 
func resize(size:CGSize, contentMode: UIImageContentMode = .ScaleToFill) -> UIImage? 

屏幕密度

// To create an image that is Retina aware, use the screen scale as a multiplier for your size. You should also use this technique for padding or borders. 
let width = 140 * UIScreen.mainScreen().scale 
let height = 140 * UIScreen.mainScreen().scale 
let image = UIImage(named: "myImage")?.resize(CGSize(width: width, height: height)) 

而且这样的东西:图像效果

// Applies a light blur effect to the image 
func applyLightEffect() -> UIImage? 
// Applies a extra light blur effect to the image 
func applyExtraLightEffect() -> UIImage? 
// Applies a dark blur effect to the image 
func applyDarkEffect() -> UIImage? 
// Applies a color tint to an image 
func applyTintEffect(tintColor: UIColor) -> UIImage? 
// Applies a blur to an image based on the specified radius, tint color saturation and mask image 
func applyBlur(blurRadius:CGFloat, tintColor:UIColor?, saturationDeltaFactor:CGFloat, maskImage:UIImage? = nil) -> UIImage? 
1
-(UIImage *)getNeedImageFrom:(UIImage*)image cropRect:(CGRect)rect 
{ 

    CGImageRef subImage = CGImageCreateWithImageInRect(image.CGImage, rect); 
    UIImage *croppedImage = [UIImage imageWithCGImage:subImage]; 
    CGImageRelease(subImage); 
    return croppedImage; 
} 

调用

UIImage *imageSample=image; 
    CGRect rectMake1=CGRectMake(0, 0,imageSample.size.width*1/4, imageSample.size.height); 
    UIImage *img1=[[JRGlobal sharedInstance] getNeedImageFrom:imageSample cropRect:rectMake1]; 
相关问题