iOS UIImage根据屏宽调整size
来源:互联网 发布:js修改div display 编辑:程序博客网 时间:2024/05/29 03:29
需求:UIImage根据屏幕宽度按照自己本身比例改变高度
上代码,为UIImage创建一个Category
#import "UIImage+UIImageExtras.h"@implementation UIImage (UIImageExtras)- (UIImage *)imageByScalingToSize:(CGSize)targetSize{ UIImage *sourceImage = self; UIImage *newImage = nil; CGSize imageSize = sourceImage.size; CGFloat width = imageSize.width; CGFloat height = imageSize.height; CGFloat targetWidth = targetSize.width; CGFloat targetHeight = targetSize.height; CGFloat scaleFactor = 0.0; CGFloat scaledWidth = targetWidth; CGFloat scaledHeight = targetHeight; CGPoint thumbnailPoint = CGPointMake(0.0,0.0); if (CGSizeEqualToSize(imageSize, targetSize) ==NO) { CGFloat widthFactor = targetWidth / width; CGFloat heightFactor = targetHeight / height; if (widthFactor < heightFactor) scaleFactor = widthFactor; else scaleFactor = heightFactor; scaledWidth = width * scaleFactor; scaledHeight = height * scaleFactor; // center the image if (widthFactor < heightFactor) { thumbnailPoint.y = (targetHeight - scaledHeight) * 0.5; } else if (widthFactor > heightFactor) { thumbnailPoint.x = (targetWidth - scaledWidth) * 0.5; } } // this is actually the interesting part: UIGraphicsBeginImageContext(targetSize); CGRect thumbnailRect = CGRectZero; thumbnailRect.origin = thumbnailPoint; thumbnailRect.size.width = scaledWidth; thumbnailRect.size.height = scaledHeight; [sourceImage drawInRect:thumbnailRect]; newImage =UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); if(newImage == nil) NSLog(@"could not scale image"); return newImage ;}@end在需要使用的地方import然后使用
CGSize size = image.size; image = [image imageByScalingToSize:CGSizeMake([UIScreen mainScreen].bounds.size.width,[UIScreen mainScreen].bounds.size.width * (size.height / size.width))]; self.imageview.image = image;
0 0
- iOS UIImage根据屏宽调整size
- 根据size调整图片背景
- UIImage--size
- ios图片缩放,根据UIImageView Frame的尺寸,自动调整UIImage缩放,保证图片显示不变形
- [iOS代码] 根据文字生成图片UIImage
- iOS 根据颜色值来生成UIImage
- 根据viewport的size自动调整fontsize大小
- IOS中测量和调整UIImage到指定CGSize
- IOS常用第三方框架 --- UIImage+Resize 调整图片大小
- uiimage的size属性
- 调整UIImage的大小
- ios UIImage
- UIImage ios
- UIImage ios
- IOS UIImage
- iOS---UIImage
- 【代码笔记】iOS-根据size截取屏幕中间矩形区域
- iOS开发--根据文字动态调整按钮(UIButton)宽度
- Gitlab修改用户密码
- nyoj108——士兵杀敌(一)
- hibernate 的 inverse 和 cascade
- HashMap和Hashtable的区别
- java环境搭建
- iOS UIImage根据屏宽调整size
- mysql数据库的基础操作sql应用
- SQL 修改表的信息 ALTER 语法总结
- library not found for -lWeChatSDK
- Codevs 1404 字符串匹配(Kmp)
- 文档管理app端需求实现方案关键点记录
- hive安装——metadata存储在mysql中
- 设计模式-建造者模式
- ArrayList和Vector区别