0

可能重复:
调整 UIImage 大小的最简单方法?

我正在使用 ipod 设备的相机来捕捉图像,在拍摄图像后,我想根据用户在我的应用程序中的需要调整它的大小。但是通过在相机代码中启用编辑:true,它仅以图像中间部分的矩形格式显示图像。在此先感谢您的帮助。请注意,我想在此处裁剪图像。这意味着图像根据裁剪图像精确调整大小。

4

2 回答 2

1

这可能会帮助你

- (UIImage*)imageByScalingAndCroppingForSize:(CGSize)targetSize AndImage:(UIImage*)sourceImage
{
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; // 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 = (targetHeight - scaledHeight) * 0.5; 
    }
    else 
        if (widthFactor < heightFactor)
        {
            thumbnailPoint.x = (targetWidth - scaledWidth) * 0.5;
        }
}       

UIGraphicsBeginImageContext(targetSize); // this will crop

CGRect thumbnailRect = CGRectZero;
thumbnailRect.origin = thumbnailPoint;
thumbnailRect.size.width  = scaledWidth;
thumbnailRect.size.height = scaledHeight;

[sourceImage drawInRect:thumbnailRect];

newImage = UIGraphicsGetImageFromCurrentImageContext();
if(newImage == nil) 
NSLog(@"could not scale image");

//pop the context to get back to the default
UIGraphicsEndImageContext();
return newImage;
}
于 2012-12-28T07:31:36.263 回答
1

在这里您可以调整图像大小。

UIImage* resizedImage(UIImage *inImage, CGRect thumbRect)
{
CGImageRef          imageRef = [inImage CGImage];
CGImageAlphaInfo    alphaInfo = CGImageGetAlphaInfo(imageRef);

// There's a wierdness with kCGImageAlphaNone and CGBitmapContextCreate
// see Supported Pixel Formats in the Quartz 2D Programming Guide
// Creating a Bitmap Graphics Context section
// only RGB 8 bit images with alpha of kCGImageAlphaNoneSkipFirst, kCGImageAlphaNoneSkipLast, kCGImageAlphaPremultipliedFirst,
// and kCGImageAlphaPremultipliedLast, with a few other oddball image kinds are supported
// The images on input here are likely to be png or jpeg files
if (alphaInfo == kCGImageAlphaNone)
    alphaInfo = kCGImageAlphaNoneSkipLast;

// Build a bitmap context that's the size of the thumbRect
CGContextRef bitmap = CGBitmapContextCreate(
            NULL,
            thumbRect.size.width,       // width
            thumbRect.size.height,      // height
            CGImageGetBitsPerComponent(imageRef),   // really needs to always be 8
            4 * thumbRect.size.width,   // rowbytes
            CGImageGetColorSpace(imageRef),
            alphaInfo
    );

// Draw into the context, this scales the image
CGContextDrawImage(bitmap, thumbRect, imageRef);

// Get an image from the context and a UIImage
CGImageRef  ref = CGBitmapContextCreateImage(bitmap);
UIImage*    result = [UIImage imageWithCGImage:ref];

CGContextRelease(bitmap);   // ok if NULL
CGImageRelease(ref);

return result;

}

希望,它会有所帮助。

于 2012-12-28T07:32:07.490 回答