根据UIImage创建任意大小thumbnail
来源:互联网 发布:万达 知乎 编辑:程序博客网 时间:2024/06/03 19:08
来源:苹果开发中文网 www.cocoadev.cn
+ (UIImage*)thumbnailOfImage:(UIImage*)image withSize:(CGSize)aSize
{
//NSLog(@"create thumbnail image");
if (!image)
return nil;
CGImageRef imageRef = [image CGImage];
UIImage *thumb = nil;
//struct CGSize imgSize = [imageRef size];
float _width = CGImageGetWidth(imageRef);
float _height = CGImageGetHeight(imageRef);
// hardcode width and height for now, shouldn't stay like that
float _resizeToWidth;
float _resizeToHeight;
_resizeToWidth = aSize.width;
_resizeToHeight = aSize.height;
float _moveX = 0.0f;
float _moveY = 0.0f;
// determine the start position in the window if it doesn't fit the sizes 100%
//NSLog(@" width: %f to: %f", _width, _resizeToWidth);
//NSLog(@" height: %f to: %f", _height, _resizeToHeight);
// resize the image if it is bigger than the screen only
if ( (_width > _resizeToWidth) || (_height > _resizeToHeight) )
{
float _amount = 0.0f;
if (_width > _resizeToWidth)
{
_amount = _resizeToWidth / _width;
_width *= _amount;
_height *= _amount;
//NSLog(@"1 width: %f height: %f", _width, _height);
}
if (_height > _resizeToHeight)
{
_amount = _resizeToHeight / _height;
_width *= _amount;
_height *= _amount;
//NSLog(@"2 width: %f height: %f", _width, _height);
}
}
_width = (NSInteger)_width;
_height = (NSInteger)_height;
_resizeToWidth = _width;
_resizeToHeight = _height;
CGContextRef bitmap = CGBitmapContextCreate(
NULL,
_resizeToWidth,
_resizeToHeight,
CGImageGetBitsPerComponent(imageRef),
CGImageGetBitsPerPixel(imageRef)*_resizeToWidth,
CGImageGetColorSpace(imageRef),
CGImageGetBitmapInfo(imageRef)
);
// now center the image
_moveX = (_resizeToWidth - _width) / 2;
_moveY = (_resizeToHeight - _height) / 2;
CGContextSetRGBFillColor(bitmap, 1.f, 1.f, 1.f, 1.0f);
CGContextFillRect( bitmap, CGRectMake(0, 0, _resizeToWidth, _resizeToHeight));
//CGContextRotateCTM( bitmap, 180*(M_PI/180));
CGContextDrawImage( bitmap, CGRectMake(_moveX, _moveY, _width, _height), imageRef );
// create a templete imageref.
CGImageRef ref = CGBitmapContextCreateImage( bitmap );
thumb = [UIImage imageWithCGImage:ref];
// release the templete imageref.
CGContextRelease( bitmap );
CGImageRelease( ref );
return [[thumb retain] autorelease];
}
- 根据UIImage创建任意大小thumbnail
- 根据UIImage创建任意大小thumbnail
- 根据UIImage创建任意大小thumbnail
- 创建任意大小数组
- JAVA 创建任意大小的文件
- python创建任意大小的文件
- 37-给UIImage,NSString添加扩展方法(根据内容拉伸图片,根据内容计算文本大小)
- 根据屏幕大小来创建布局
- [Shell学习笔记] 创建任意大小的文件And分割任意大小的文件
- uiimage 變動圖像的大小
- 修改UIImage的大小
- 修改UIImage大小
- 修改UIImage的大小
- 修改UIImage的大小
- 调整UIImage的大小
- 修改UIImage的大小
- IOS 修改UIImage大小
- 改变UIImage的大小
- VC usb编程从0开始.
- Ubuntu下千千静听Audacious的安装步骤详解
- Linux系统安装之二: 硬盘安装Fedora 9
- iPhone UIImage保存到文件,PNG/JPEG自适应
- HOST文件详解
- 根据UIImage创建任意大小thumbnail
- 建网站的那些事(4)--- 网站域名
- 今天我正式开始我的csdn之旅!!!
- LIMS系统开发经历----之一(需求分析(初步))
- 调用getNextException查看原因(转)
- 求平面切割三角形的交线
- iPhone下旋转UIImage
- Pwn2Own 2010即将开幕 Chrome能否成功卫冕?
- 为何要用封装(Encapsulation)