UIImageView圆角,自适应图片宽高比例,图片拉伸,缩放比例和图片缩微图
2016-03-31 20:22
555 查看
/*
设置圆角,通过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];
//缩微图
- (UIImage *)generatePhotoThumbnail:(UIImage *)image {
// Create a thumbnail version of the image for the event object.
CGSize size = image.size;
CGSize croppedSize;
CGFloat ratioX = 75.0;
CGFloat ratioY = 60.0;
CGFloat offsetX = 0.0;
CGFloat offsetY = 0.0;
// check the size of the image, we want to make it
// a square with sides the size of the smallest dimension
if (size.width > size.height) {
offsetX = (size.height - size.width) / 2;
croppedSize = CGSizeMake(size.height, size.height);
} else {
offsetY = (size.width - size.height) / 2;
croppedSize = CGSizeMake(size.width, size.width);
}
// Crop the image before resize
CGRect clippedRect = CGRectMake(offsetX * -1, offsetY * -1, croppedSize.width, croppedSize.height);
CGImageRef imageRef = CGImageCreateWithImageInRect([image CGImage], clippedRect);
// Done cropping
// Resize the image
CGRect rect = CGRectMake(0.0, 0.0, ratioX, ratioY); // 设置图片缩微图的区域((0,0),宽:75 高:60)
UIGraphicsBeginImageContext(rect.size);
[[UIImage imageWithCGImage:imageRef] drawInRect:rect];
UIImage *thumbnail = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
// Done Resizing
return thumbnail;
}
设置圆角,通过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];
//缩微图
- (UIImage *)generatePhotoThumbnail:(UIImage *)image {
// Create a thumbnail version of the image for the event object.
CGSize size = image.size;
CGSize croppedSize;
CGFloat ratioX = 75.0;
CGFloat ratioY = 60.0;
CGFloat offsetX = 0.0;
CGFloat offsetY = 0.0;
// check the size of the image, we want to make it
// a square with sides the size of the smallest dimension
if (size.width > size.height) {
offsetX = (size.height - size.width) / 2;
croppedSize = CGSizeMake(size.height, size.height);
} else {
offsetY = (size.width - size.height) / 2;
croppedSize = CGSizeMake(size.width, size.width);
}
// Crop the image before resize
CGRect clippedRect = CGRectMake(offsetX * -1, offsetY * -1, croppedSize.width, croppedSize.height);
CGImageRef imageRef = CGImageCreateWithImageInRect([image CGImage], clippedRect);
// Done cropping
// Resize the image
CGRect rect = CGRectMake(0.0, 0.0, ratioX, ratioY); // 设置图片缩微图的区域((0,0),宽:75 高:60)
UIGraphicsBeginImageContext(rect.size);
[[UIImage imageWithCGImage:imageRef] drawInRect:rect];
UIImage *thumbnail = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
// Done Resizing
return thumbnail;
}
相关文章推荐
- UITableView使用指南(呕心原创)
- 使用CAShapeLayer与UIBezierPath画出想要的图形
- Burp suite与fuzzdb
- Burp Suite使用介绍(一)
- ob_flush()和flush()和ob_implicit_flush(true)
- warning: Error opening file (../../modules/highgui/src/cap_ffmpeg_impl.hpp:545)
- 在项目当中如何对UIButton进行重新布局
- javaGUI中 随机生成一组位置不同的按钮 并对JButton的左键、右键、双击的监听
- IOS开发之UIView总结
- easyui 点击文字展开和收缩节点
- ABP源码分析十八:UI Inputs
- Why I can’t convert my Windows Server 2012 R2 Core to GUI
- UIBezierPath - 贝塞尔曲线
- Ubuntu15.10安装Arduino IDE记录
- android自定义UI组件
- UITableViewController和XML解析还有地图的简单结合
- easyui tree 增加参数
- #调整随机森林的参数(调整n_estimators随机森林中树的数量默认10个树,精度递增显著,但并不是越多越好),加上verbose=True,显示进程使用信息
- miniGUI3.0.12移植到Hi3531
- miniGUI3.0.12移植到Hi3531