1

アプリの画像のサムネイルを作成しようとしています。私はCGImageSourceCreateThumbnailAtIndexを使用しています。私が見ている 1 つの問題は、オプションのkCGImageSourceThumbnailMaxPixelSizeが幅と高さの両方の最大ピクセル サイズであることです。

私が抱えている問題は、500w x 1000h の画像があり、それを 250w x 500h に縮小したいということです。CGImageSourceCreateThumbnailAtIndexを使用すると、幅を 250 に指定したいのですが、代わりに高さが 250 になっているようです。

これを使用して、長方形の画像を幅で縮小するにはどうすればよいですか?

ありがとう!

4

2 に答える 2

2

kCGImageSourceCreateThumbnailWithTransform指定された Optionsのキーに値 (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);

この実装を変更して、サイズの代わりに必要な最大値を提供できます

ImageIO を使用してサイズを変更するために UIImage に追加されたカテゴリ

UIImage+Resizing.h

#import <UIKit/UIKit.h>

@interface UIImage (Resizing)

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

@end

UIImage+Resizing.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/

そして、Googleはあなたの親友になることができます。

于 2013-04-30T06:56:53.430 に答える