UIImageView圆角,自适应图片宽高比例,图片拉伸,缩放比例

转自 http://blog.csdn.net/nogodoss/article/details/7395802

/*
     设置圆角,通过layer中的cornerRadius和masksToBounds即可。
     
     自适应图片宽高比例。通过UIViewContentModeScaleAspectFit设置,注意这个UIImageView的frame就不是init中的数据了。
     
     同样的UIImage图片放入不同frame中的UIImageView就可以实现比例缩放了。只是UIImageView的大小改变了,

     
     */
    UIImage* image = [UIImage imageNamed:@"back2.jpg"];   
    UIImageView* imageView1 = [[[UIImageView alloc] initWithImage:image] autorelease];   
    imageView1.frame = CGRectMake(0, 0, 300, 200);   
    imageView1.center = CGPointMake(150, 200);
    //设置圆角
    imageView1.layer.cornerRadius = 8;  
    imageView1.layer.masksToBounds = YES;
    
    //自适应图片宽高比例
    imageView1.contentMode = UIViewContentModeScaleAspectFit;  
    [self.view addSubview:imageView1];  
    
    
    //拉伸图片
    CGFloat capWidth = image.size.width / 2;  
    CGFloat capHeight = image.size.height / 2;  
    UIImage* stretchableImage = [image stretchableImageWithLeftCapWidth:capWidth topCapHeight:capHeight];
    UIImageView* imageView3 = [[[UIImageView alloc] initWithImage:stretchableImage] autorelease];
    imageView3.frame = CGRectMake(0, 0, 300, 200);   
    imageView3.center = CGPointMake(150, 200);  
    [self.view addSubview:imageView3];
    
    //改变frame改变
    UIImageView* imageView4 = [[[UIImageView alloc] initWithImage:image] autorelease];
    imageView4.frame = CGRectMake(0, 0, 300/2, 200/2);   
    imageView4.center = CGPointMake(150, 200);  
    [self.view addSubview:imageView4];

你可能感兴趣的:(图片,imageview,uiimage,UIImageView)