1

我正在尝试为我的应用创建图像缩略图。我正在使用CGImageSourceCreateThumbnailAtIndex来做到这一点。我看到的一个问题是,对于选项,kCGImageSourceThumbnailMaxPixelSize是宽度和高度的最大像素大小。

我遇到的问题是:我有一个 500w x 1000h 的图像,我想将其缩小为 250w x 500h。当我使用CGImageSourceCreateThumbnailAtIndex时,我想将宽度指定为 250,但它似乎将我的高度设置为 250。

如何使用它按宽度缩小矩形图像?

谢谢!

4

2 回答 2

2

kCGImageSourceCreateThumbnailWithTransform为提供给的选项中的键提供值 (kCFBooleanTrue),CGImageSourceCreateThumbnailAtIndex缩略图应根据完整图像的方向和像素纵横比进行旋转和缩放。

这是此密钥的文档

/* Specifies whether the thumbnail should be rotated and scaled according
 * to the orientation and pixel aspect ratio of the full image. The value
 * of this key must be a CFBooleanRef; the default value of this key is 
 * kCFBooleanFalse. */

IMAGEIO_EXTERN const CFStringRef kCGImageSourceCreateThumbnailWithTransform  IMAGEIO_AVAILABLE_STARTING(__MAC_10_4, __IPHONE_4_0);

您可以修改此实现以提供所需的最大值而不是大小

添加到 UIImage 的类别以使用 ImageIO 调整其大小

UIImage+调整大小.h

#import <UIKit/UIKit.h>

@interface UIImage (Resizing)

-(UIImage*)resizedImageWithData:(NSData*)imageData withSize:(CGSize)size;

@end

UIImage+调整大小.m

#import "UIImage+Resizing.h"
#import <ImageIO/ImageIO.h>

@implementation UIImage (Resizing)

-(UIImage*)resizedImageWithData:(NSData*)imageSource withSize:(CGSize)size{
    //  Resizing Using ImageIO
    CGImageSourceRef src = CGImageSourceCreateWithData((__bridge CFDataRef)imageSource, nil);

    // load the image at the desired size
    NSDictionary* options = @{
                        (id)kCGImageSourceShouldAllowFloat: (id)kCFBooleanTrue,
                        (id)kCGImageSourceCreateThumbnailWithTransform: (id)kCFBooleanTrue,
                        (id)kCGImageSourceCreateThumbnailFromImageAlways: (id)kCFBooleanTrue,
                        (id)kCGImageSourceThumbnailMaxPixelSize: @((int)(size.width > size.height ? size.width : size.height))
                        };

    CGImageRef imageRef = CGImageSourceCreateThumbnailAtIndex(src, 0, (__bridge CFDictionaryRef)options);
    if (NULL != src)
        CFRelease(src);

    UIImage* scaledImage = [UIImage imageWithCGImage:imageRef];
    if (NULL != imageRef)
        CFRelease(imageRef);

    return scaledImage;
}

@end
于 2016-12-29T13:08:06.827 回答
-3

有很多方法可以调整图像大小,如果你谷歌它,你可以找到很多..

- (UIImage *)resizeImage:(UIImage*)image newSize:(CGSize)newSize {
    CGRect newRect = CGRectIntegral(CGRectMake(0, 0, newSize.width, newSize.height));
    CGImageRef imageRef = image.CGImage;

    UIGraphicsBeginImageContextWithOptions(newSize, NO, 0);
    CGContextRef context = UIGraphicsGetCurrentContext();


    CGContextSetInterpolationQuality(context, kCGInterpolationHigh);
    CGAffineTransform flipVertical = CGAffineTransformMake(1, 0, 0, -1, 0, newSize.height);

    CGContextConcatCTM(context, flipVertical);  
    // Draw into the context; this scales the image
    CGContextDrawImage(context, newRect, imageRef);

    // Get the resized image from the context and a UIImage
    CGImageRef newImageRef = CGBitmapContextCreateImage(context);
    UIImage *newImage = [UIImage imageWithCGImage:newImageRef];

    CGImageRelease(newImageRef);
    UIGraphicsEndImageContext();    

    return newImage;
}

只需尝试这些链接以获取更多信息http://vocaro.com/trevor/blog/2009/10/12/resize-a-uiimage-the-right-way/

谷歌可以成为你最好的朋友。

于 2013-04-30T06:56:53.430 回答