4

境界線 (UIGraphics) で円形の画像を作成するには?

PS絵を描く必要があります。

コードviewDidLoad:

NSURL *url2 = [NSURL URLWithString:@"http://images.ak.instagram.com/profiles/profile_55758514_75sq_1399309159.jpg"];
NSData *data2 = [NSData dataWithContentsOfURL:url2];
UIImage *profileImg = [UIImage imageWithData:data2];

UIGraphicsEndImageContext();
// Create image context with the size of the background image.
UIGraphicsBeginImageContext(profileImg.size);
[profileImg drawInRect:CGRectMake(0, 0, profileImg.size.width, profileImg.size.height)];

// Get the newly created image.
UIImage *result = UIGraphicsGetImageFromCurrentImageContext();

// Release the context.
UIGraphicsEndImageContext();

// Set the newly created image to the imageView.
self.imageView.image = result;
4

3 に答える 3

7

これを試しましたか?

self.imageView.layer.borderColor = [UIColor greenColor].CGColor;
self.imageView.layer.borderWidth = 1.f;

また、必要になります

self.imageView.layer.corderRadius = self.imageView.frame.size.width/2;
self.imageView.clipsToBounds = YES;
于 2014-08-22T14:28:01.290 に答える
1

スウィフト 4 バージョン

extension UIImage {

    func circularImageWithBorderOf(color: UIColor, diameter: CGFloat, boderWidth:CGFloat) -> UIImage {
         let aRect = CGRect.init(x: 0, y: 0, width: diameter, height: diameter)
         UIGraphicsBeginImageContextWithOptions(aRect.size, false, self.scale)

         color.setFill()
         UIBezierPath.init(ovalIn: aRect).fill()

         let anInteriorRect = CGRect.init(x: boderWidth, y: boderWidth, width: diameter-2*boderWidth, height: diameter-2*boderWidth)
         UIBezierPath.init(ovalIn: anInteriorRect).addClip()

         self.draw(in: anInteriorRect)

         let anImg = UIGraphicsGetImageFromCurrentImageContext()!
         UIGraphicsEndImageContext()

         return anImg
     }
}
于 2018-03-17T20:27:53.957 に答える