iOS学习-压缩图片(改变图片的宽高)
2015-09-21 14:38
495 查看
压缩图片,图片的大小与我们期望的宽高不一致时,我们可以将其处理为我们想要的宽高。
传入想要修改的图片,以及新的尺寸
-(UIImage*)imageWithImage:(UIImage*)image scaledToSize:(CGSize)newSize { // Create a graphics image context UIGraphicsBeginImageContext(newSize); // Tell the old image to draw in this new context, with the desired // new size [image drawInRect:CGRectMake(0,0,newSize.width,newSize.height)]; // Get the new image from the context UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext(); // End the context UIGraphicsEndImageContext(); // Return the new image. return newImage; }
像button自带的imageView属赋图片时,若图片过大会自动压缩,导致失真(有时不能小到我们期望的)。如我在iOS学习-UIButton的imageView和titleLabel中
宽高够大时是没有问题的,但我把button的titleLabel.font设小(12),宽高设小(高度设为30或更小)时就有问题了
,是不是觉得后面的图片太大了,
而图片的宽高又不好直接改,这时我们就可以先把图片给做小点,在加上就OK了。
UIImage *buttonImage = [UIImage imageNamed:image]; //压缩图片大小 buttonImage = [self imageWithImage:buttonImage scaledToSize:CGSizeMake(20, 20)]; CGFloat buttonImageViewWidth = CGImageGetWidth(buttonImage.CGImage) ; CGFloat buttonImageViewHeight = CGImageGetHeight(buttonImage.CGImage); NSString *buttonTitle = title; UIFont *buttonTitleFont = [UIFont boldSystemFontOfSize:12.0f];
相关文章推荐
- iOS开发遇到的坑之一: 开发遇见如下错误:Undefined symbols for architecture arm64
- IOS图片缓存
- 如何实现iOS图书动画:第1部分(上)
- 关于页面跳转过程中出现两个界面view出现重叠的处理
- iOS Xcode: linker command failed with exit code 1 (use -v to see invocation) 处理方法
- ios开发日记11 对tableView三种计算动态行高方法的分析
- 自定义iOS的Back按钮(backBarButtonItem)和pop交互手势(interactivepopgesturerecognizer)
- 统一设计,iOS6也玩扁平化
- iOS:hidesBottomBarWhenPushed的正确用法
- iOS-设置状态栏白色以及覆盖状态栏
- iOS 9 xcode7 适配问题
- iOS 如何实现View的复用(以访问相册为例)
- ios NSDictionary\NSMutableDictionary
- iOS 日期格式 星期几 本地
- iOS集成支付宝
- iOSCoreAnimation动画系列教程(一):CABasicAnimation【包会】
- Thinkpad X240修改bios引导方式
- iOS集成支付宝
- Mac iOS推送测试
- iOS应用开发之地图位置解析