Rounded Corners on UIImage

前端 未结 12 978
终归单人心
终归单人心 2020-12-02 04:08

I\'m trying to draw images on the iPhone using with rounded corners, a la the contact images in the Contacts app. I\'ve got code that generally work, but it occasionally cra

相关标签:
12条回答
  • 2020-12-02 04:42

    If it only crashes some of the time, figure out what the crash cases have in common. Is dstRect the same every time? Are the images ever a different size?

    Also, you need to CGPathRelease(borderPath), although I doubt that leak is causing your problem.

    0 讨论(0)
  • 2020-12-02 04:45

    Set the Image in xib or storyboard (image width and height 41x41).

    FirstViewController.h

    @interface....
    
    IBOutlet UIImageView *testImg;
    
    @end
    

    FirstViewController.m

    -(void)viewDidLoad{
            testImg.layer.backgroundColor=[[UIColor clearColor] CGColor];
            testImg.layer.cornerRadius=20;
            testImg.layer.masksToBounds = YES;
        }
    
    0 讨论(0)
  • 2020-12-02 04:47

    In Swift 4.2 and Xcode 10.1

    let imgView = UIImageView()
    imgView.frame = CGRect(x: 200, y: 200, width: 200, height: 200)
    imgView.image = UIImage(named: "yourimagename")
    imgView.imgViewCorners()
    //If you want complete round shape
    //imgView.imgViewCorners(width: imgView.frame.width)//Pass ImageView width
    view.addSubview(imgView)
    
    extension UIImageView {
    //If you want only round corners
    func imgViewCorners() {
        layer.cornerRadius = 10
        layer.borderWidth = 1.0
        layer.borderColor = UIColor.red.cgColor
        layer.masksToBounds = true
    }
    //If you want complete round shape
    func imgViewCorners(width:CGFloat) {
        layer.cornerRadius = width/2
        layer.borderWidth = 1.0
        layer.borderColor = UIColor.red.cgColor
        layer.masksToBounds = true
    }
    
    0 讨论(0)
  • 2020-12-02 04:49

    The easiest way is to embed a disabled[!] round-rect [not custom!] button in your view (can even do it all in the Interface Builder) and associate your image with it. The image-setting message is different for UIButton (compared to UIImageView), but the overall kludge works like a charm. Use setImage:forState: if you want a centered icon or setBackgroundImage:forState: if you want the whole image with corners cut (like Contacts). Of course if you want to display lots of these images in your drawRect this isn't the right approach, but more likely an embedded view is exactly what you needed anyway...

    0 讨论(0)
  • 2020-12-02 04:50
    UIImage *originalImage = [UIImage imageNamed:@"OriginalImage.png"] 
    [self performSelectorOnMainThread:@selector(displayImageWithRoundedCorners:) withObject:originalImage waitUntilDone:YES];
    
    0 讨论(0)
  • 2020-12-02 04:52

    I cant offer any insight into your crash, but I thought I would offer another option for rounding the corners. I had a similar problem arise in an application i was working on. Rather than write any code I am overlaying another image which masks off the corners.

    0 讨论(0)
提交回复
热议问题