Making a UIImage to a circle form

后端 未结 10 2080
南旧
南旧 2020-12-02 17:16

I have been trying to mask a UIImage into a circle. I\'m using now the code that has been popular on other answers here, but although I do get a circle its edges are very ja

10条回答
  •  长情又很酷
    2020-12-02 17:39

    If you are using an UIImageView and you are looking to animate the size of the image while maintaining the rounded corners, you can apply a transformation as follows:

    Objective-C

    // Our UIImageView reference
    @property (weak, nonatomic) IBOutlet UIImageView *myImageView;
    
    - (void)viewDidLoad {
        [super viewDidLoad];
    
         myImageView.layer.cornerRadius = myImageView.frame.size.width / 2.0f;
         myImageView.transform = CGAffineTransformScale(CGAffineTransformIdentity, 0.4f, 0.4f);
    }
    
    - (void)viewDidAppear:(BOOL)animated {
        [UIView animateWithDuration:1.0 animations:^{
            myImageView.transform = CGAffineTransformIdentity;
        }];
    }
    

    Swift

    // Our UIImageView reference
    @IBOutlet weak var myImageView:UIImageView!
    
    override func viewDidLoad() {
        super.viewDidLoad()
    
        myImageView.layer.cornerRadius = myImageView.frame.size.width / 2;
        myImageView.transform = CGAffineTransformScale(CGAffineTransformIdentity, 0.4, 0.4)
    }
    
    override func viewDidAppear(animated: Bool) {
        UIView.animateWithDuration(1.0) {
            myImageView.transform = CGAffineTransformIdentity;
        }
    }
    

    NOTE: This will also maintain any AutoLayout constraints.

提交回复
热议问题