【问题标题】:iOS: ImageIO resizing on a rectangular imageiOS:ImageIO 在矩形图像上调整大小
【发布时间】:2013-04-29 17:43:14
【问题描述】:

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

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

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

谢谢!

【问题讨论】:

    标签: iphone ios objective-c javax.imageio


    【解决方案1】:

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

    这是此密钥的文档

    /* 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+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
    

    【讨论】:

      【解决方案2】:

      调整图片大小的方法有很多,你google一下就可以找到很多..

      - (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 可以成为你最好的朋友。

      【讨论】:

      • 谢谢,当然我已经用谷歌搜索过了,是的,有很多方法。我的问题是特定于能够使用 CGImageSourceCreateThumbnailAtIndex 来做到这一点,而不仅仅是任何方式。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-02-08
      • 2021-08-15
      • 2012-10-06
      相关资源
      最近更新 更多