我怎样才能设置图像循环

时间:2015-03-04 11:51:39

标签: ios

我已经完成了以下代码,但它不起作用。它以圆形显示我,但以裁剪图像格式显示。

我想让图片适合圈子。

UIImage * defaultImage = [UIImage imageNamed:@"empty.png"];
self.myImageView = [[UIImageView alloc] initWithImage:defaultImage];

CGRect myFrame = CGRectMake(90.0f, 100.0f, self.myImageView.frame.size.width/1.5,self.myImageView.frame.size.height/1.5);
[self.myImageView setFrame:myFrame];

[self.myImageView setContentMode:UIViewContentModeScaleAspectFit];

self.myImageView.layer.masksToBounds = NO;
self.myImageView.layer.cornerRadius = self.myImageView.frame.size.width/2;       
self.myImageView.layer.borderColor = [UIColor blackColor].CGColor;
self.myImageView.clipsToBounds=YES;

self.myImageView.layer.borderWidth = 2.0f;

[self.view addSubview:self.myImageView];

3 个答案:

答案 0 :(得分:1)

您可以使用此方法

-(void)setRoundedView:(UIImageView *)roundedView toDiameter:    
(float)newSize;
{
    CGPoint saveCenter = roundedView.center;
    CGRect newFrame = CGRectMake(roundedView.frame.origin.x, 
    roundedView.frame.origin.y, newSize, newSize);
    roundedView.frame = newFrame;
    roundedView.layer.cornerRadius = newSize / 2.0;
    roundedView.center = saveCenter;
}

现在使用您的imageview调用方法

[self setRoundedView:self.myImageView toDiameter:self.myImageView.frame.size.width];
 self.myImageView.clipsToBounds = YES;

希望有所帮助

答案 1 :(得分:0)

简而言之,

anyView.layer.cornerRadius = anyView.frame.size.width/2;

Make sure that clipsToBound property of that view is enabled.

答案 2 :(得分:-1)

设置宽度&高度等于你的imageView,然后将imageView的cornerRadius设置为宽度的一半&高度。

[self createRoundUIView:self.userProfileImageView sizeDiameter:self.userProfileImageView.frame.size.width];

并将以下代码添加到bellow方法

-(void)createRoundUIView:(UIImageView *)inputView sizeDiameter:(float)diameterSize
{
  CGPoint saveCenter = inputView.center;
  CGRect frame = CGRectMake(inputView.frame.origin.x, inputView.frame.origin.y, diameterSize, diameterSize);
  inputView.frame = frame;
  inputView.layer.cornerRadius = diameterSize / 2.0;
  inputView.center = saveCenter;
  inputView.clipsToBounds = YES;
}