2016-09-23 10 views
14

目標:私は次のコードを実行UIImage(つまり2.0のscale財産で始まる)スケールプロパティを失うことなくUIImageをトリミングする方法は?

クロップ:

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

このコードは動作しますが、しかしその結果、croppedUIImageは、不正確なscale性質を持っています1.0。

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

これは正しいスケールを生成するが、それは間違って半分にsize寸法をカット:


私は、最終的なイメージを作成するときにscale指定しようとしました。


ここで何をすればよいですか?

(*注:私は後でscaleプロパティの影響を受けているUIImagePNGRepresentation(_ image: UIImage)で画像を保存するためにUIImage上scaleプロパティが重要です)



編集:

私が得ました次の作業を行います。残念ながら、これはちょうど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

使用この拡張: - 私はiOSとtvOSためImageHelperポッドを使用したことだし、それは完全に取り組んでいるとも合うかもしれません

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

をあなたの要望。

それはのような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]; 
関連する問題