(网络摘抄)UIImage指定大小裁剪图片
2016-05-27 13:58
393 查看
/**
*
图片自适应大小处理
*
* @param sourceImage
原图
* @param targetSize
需要适应的大小
*
* @return 返回适应大小后的图片
*/
- (UIImage*)imageWithImage:(UIImage*)sourceImage scaledToSizeWithSameAspectRatio:(CGSize)targetSize
{
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;
// scale to fit height
}
else {
scaleFactor = heightFactor;
// scale to fit width
}
scaledWidth = width * scaleFactor;
scaledHeight = height * scaleFactor;
// center the image
if (widthFactor > heightFactor) {
thumbnailPoint.y
eea9
= (targetHeight - scaledHeight) *
0.5;
}
else
if (widthFactor < heightFactor) {
thumbnailPoint.x = (targetWidth - scaledWidth) *
0.5;
}
}
CGImageRef imageRef = [sourceImage
CGImage];
CGBitmapInfo bitmapInfo =
CGImageGetBitmapInfo(imageRef);
CGColorSpaceRef colorSpaceInfo =
CGImageGetColorSpace(imageRef);
if (bitmapInfo ==
kCGImageAlphaNone) {
bitmapInfo =
kCGImageAlphaNoneSkipLast;
}
CGContextRef bitmap;
if (sourceImage.imageOrientation ==
UIImageOrientationUp ||sourceImage.imageOrientation ==
UIImageOrientationDown) {
bitmap =
CGBitmapContextCreate(NULL, targetWidth, targetHeight,CGImageGetBitsPerComponent(imageRef),CGImageGetBytesPerRow(imageRef),
colorSpaceInfo, bitmapInfo);
} else {
bitmap =
CGBitmapContextCreate(NULL, targetHeight, targetWidth,CGImageGetBitsPerComponent(imageRef),CGImageGetBytesPerRow(imageRef),
colorSpaceInfo, bitmapInfo);
}
// In the right or left cases, we need to switch scaledWidth and scaledHeight,
// and also the thumbnail point
if (sourceImage.imageOrientation ==
UIImageOrientationLeft) {
thumbnailPoint =
CGPointMake(thumbnailPoint.y, thumbnailPoint.x);
CGFloat oldScaledWidth = scaledWidth;
scaledWidth = scaledHeight;
scaledHeight = oldScaledWidth;
CGContextRotateCTM (bitmap,
90);
CGContextTranslateCTM (bitmap,
0, -targetHeight);
}
else if (sourceImage.imageOrientation ==UIImageOrientationRight)
{
thumbnailPoint =
CGPointMake(thumbnailPoint.y, thumbnailPoint.x);
CGFloat oldScaledWidth = scaledWidth;
scaledWidth = scaledHeight;
scaledHeight = oldScaledWidth;
CGContextRotateCTM (bitmap, -90);
CGContextTranslateCTM (bitmap, -targetWidth,
0);
}
else if (sourceImage.imageOrientation ==
UIImageOrientationUp) {
// NOTHING
}
else if (sourceImage.imageOrientation ==
UIImageOrientationDown){
CGContextTranslateCTM (bitmap, targetWidth, targetHeight);
CGContextRotateCTM (bitmap, -180.);
}
CGContextDrawImage(bitmap,
CGRectMake(thumbnailPoint.x,thumbnailPoint.y, scaledWidth, scaledHeight), imageRef);
CGImageRef ref = CGBitmapContextCreateImage(bitmap);
UIImage* newImage = [UIImage
imageWithCGImage:ref];
CGContextRelease(bitmap);
CGImageRelease(ref);
return newImage;
}
*
图片自适应大小处理
*
* @param sourceImage
原图
* @param targetSize
需要适应的大小
*
* @return 返回适应大小后的图片
*/
- (UIImage*)imageWithImage:(UIImage*)sourceImage scaledToSizeWithSameAspectRatio:(CGSize)targetSize
{
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;
// scale to fit height
}
else {
scaleFactor = heightFactor;
// scale to fit width
}
scaledWidth = width * scaleFactor;
scaledHeight = height * scaleFactor;
// center the image
if (widthFactor > heightFactor) {
thumbnailPoint.y
eea9
= (targetHeight - scaledHeight) *
0.5;
}
else
if (widthFactor < heightFactor) {
thumbnailPoint.x = (targetWidth - scaledWidth) *
0.5;
}
}
CGImageRef imageRef = [sourceImage
CGImage];
CGBitmapInfo bitmapInfo =
CGImageGetBitmapInfo(imageRef);
CGColorSpaceRef colorSpaceInfo =
CGImageGetColorSpace(imageRef);
if (bitmapInfo ==
kCGImageAlphaNone) {
bitmapInfo =
kCGImageAlphaNoneSkipLast;
}
CGContextRef bitmap;
if (sourceImage.imageOrientation ==
UIImageOrientationUp ||sourceImage.imageOrientation ==
UIImageOrientationDown) {
bitmap =
CGBitmapContextCreate(NULL, targetWidth, targetHeight,CGImageGetBitsPerComponent(imageRef),CGImageGetBytesPerRow(imageRef),
colorSpaceInfo, bitmapInfo);
} else {
bitmap =
CGBitmapContextCreate(NULL, targetHeight, targetWidth,CGImageGetBitsPerComponent(imageRef),CGImageGetBytesPerRow(imageRef),
colorSpaceInfo, bitmapInfo);
}
// In the right or left cases, we need to switch scaledWidth and scaledHeight,
// and also the thumbnail point
if (sourceImage.imageOrientation ==
UIImageOrientationLeft) {
thumbnailPoint =
CGPointMake(thumbnailPoint.y, thumbnailPoint.x);
CGFloat oldScaledWidth = scaledWidth;
scaledWidth = scaledHeight;
scaledHeight = oldScaledWidth;
CGContextRotateCTM (bitmap,
90);
CGContextTranslateCTM (bitmap,
0, -targetHeight);
}
else if (sourceImage.imageOrientation ==UIImageOrientationRight)
{
thumbnailPoint =
CGPointMake(thumbnailPoint.y, thumbnailPoint.x);
CGFloat oldScaledWidth = scaledWidth;
scaledWidth = scaledHeight;
scaledHeight = oldScaledWidth;
CGContextRotateCTM (bitmap, -90);
CGContextTranslateCTM (bitmap, -targetWidth,
0);
}
else if (sourceImage.imageOrientation ==
UIImageOrientationUp) {
// NOTHING
}
else if (sourceImage.imageOrientation ==
UIImageOrientationDown){
CGContextTranslateCTM (bitmap, targetWidth, targetHeight);
CGContextRotateCTM (bitmap, -180.);
}
CGContextDrawImage(bitmap,
CGRectMake(thumbnailPoint.x,thumbnailPoint.y, scaledWidth, scaledHeight), imageRef);
CGImageRef ref = CGBitmapContextCreateImage(bitmap);
UIImage* newImage = [UIImage
imageWithCGImage:ref];
CGContextRelease(bitmap);
CGImageRelease(ref);
return newImage;
}
相关文章推荐
- js跨域问题之跨域iframe自适应大小实现代码
- UIImage初始化的区别两种方法介绍(面试常见)
- iOS应用开发中对UIImage进行截取和缩放的方法详解
- IOS高级教程1:处理1000张图片的内存优化
- 获取UIImage中的图像数据
- UIImage的一个实例函数stretchableImageWithLeftCapWidth
- 【无线互联】基于ios系统的图片(滤镜)编辑
- UIImageView的使用
- iOS实现头像选取(照相或者图片库)、大小等比缩放、生成圆形头像
- iOS- UIImageView/手势识别
- MBProgressHUD的基本使用
- IOS SWIFT 设置图片圆形
- UIImageView 加载高清大图时内存管理
- UIColor的RGB转16进制和转UIImage
- 图片裁剪工具类
- IOS 获取所有图片工具类
- IOS Multipage(多页面)程序开发,UITabBarController
- ios 获取当前屏幕快照
- UIImage 切圆
- 关于UIimage的一些注释