2016-06-23 12 views
0

私はここで見つけた少なくとも5つの方法を試しましたが、何も機能していません。すべて最低でも少なくとも1〜2歳は最低です。それ以来、Appleは何かルールを変更しましたか?四角形のUIImageを丸い矩形に入れます

私は、正方形にそれをトリミングし、スクリーンショットを取り、その後、私は15

の半径にその角をトリミングしようとしています多分、OpenGLのでこれを行うために、より深刻かつ確実な方法はありますか?現在、このソリューションをしようとしたが、コーナーはまだトリミングすることを拒否

-(UIImage*) cropImage:(UIImage*)image withPath:(UIBezierPath*)path { // where the UIBezierPath is defined in the UIKit coordinate system (0,0) is top left 

    CGRect r = CGPathGetBoundingBox(path.CGPath); // the rect to draw our image in (minimum rect that the path occupies). 

    UIGraphicsBeginImageContextWithOptions(r.size, NO, 0.0); // begin image context, with transparency & the scale of the image. 
    CGContextRef ctx = UIGraphicsGetCurrentContext(); 

    CGContextTranslateCTM(ctx, -r.origin.x, -r.origin.y); // translate context so that when we add the path, it starts at (0,0). 

    CGContextAddPath(ctx, path.CGPath); // add path. 
    CGContextClip(ctx); // clip any future drawing to the path region. 

    [image drawInRect:(CGRect){CGPointZero, image.size}]; // draw image 

    UIImage* i = UIGraphicsGetImageFromCurrentImageContext(); // get image from context 
    UIGraphicsEndImageContext(); // clean up and finish context 

    return i; // return image 
} 

だけ明確にするために、私は実際に私が

をトリミングする画素を削除しようとしていると私はに渡しています。 ..

[self cropImage:myImage withPath:[UIBezierPath bezierPathWithRoundedRect:CGRectMake(0, 0, screenImage.size.width, screenImage.size.height) cornerRadius:15.0]] 

コーナーの半径を大きくしても、何も起こりません。

+1

ポストを。 – Code

+0

@codeただ投稿しました – Gukki5

答えて

0

この時点で、おそらくいくつかの解決策が見つかりましたが、ここに到着する次の解決策では、この解決策が役立ちます。描画ジョブを行うのにUIImageViewを使用しているので、これはちょっとした回避策ですが、うまくいきます。

extension UIImage { 

    var circle: UIImage? { 
     let length = min(size.width, size.height) 
     let imageView = UIImageView(frame: CGRect(origin: .zero, size: CGSize(width: length, height: length))) 
     imageView.contentMode = .scaleAspectFill 
     imageView.image = self 
     imageView.layer.cornerRadius = length/2 
     imageView.layer.masksToBounds = true 
     UIGraphicsBeginImageContextWithOptions(imageView.bounds.size, false, scale) 
     guard let context = UIGraphicsGetCurrentContext() else { return nil } 
     imageView.layer.render(in: context) 
     let result = UIGraphicsGetImageFromCurrentImageContext() 
     UIGraphicsEndImageContext() 
     return result 
    } 
} 

あなたは、より堅牢なソリューションをしたい場合は、ここではこの1つが役立つかもしれない:あなたが試したものをhttps://github.com/giacgbj/UIImageSwiftExtensions/

関連する問題