您的位置:首页 > 理论基础 > 计算机网络

(网络摘抄)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;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  UIImage 自适应大小