2013-04-29 4 views
1

내 앱의 이미지 미리보기 이미지를 만들려고합니다. 나는 CGImageSourceCreateThumbnailAtIndex을 사용하고 있습니다. 내가 보는 한가지 문제점은 옵션의 경우, kCGImageSourceThumbnailMaxPixelSize은 너비와 높이의 최대 픽셀 크기입니다.iOS : 사각형 이미지의 ImageIO 크기 조정

내가 겪고있는 문제는 다음과 같습니다. 이미지가 500w x 1000h이고 250w x 500h라고 말하기 위해 크기를 조정하고 싶습니다. CGImageSourceCreateThumbnailAtIndex 너비를 250으로 지정하고 대신 내 높이 250을 만드는 것처럼 보입니다.

이 방법을 사용하여 사각형 이미지의 너비를 축소 할 수 있습니까?

감사합니다. 당신이 구글 경우

답변

-2

이미지 크기를 조정하는 방법에는 여러 가지가 있습니다, 당신은 t을 찾을 수 있습니다 ..

- (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은 당신의 가장 친한 친구가 될 수있는 추가 정보를 원하시면 다음 링크를 시도하십시오.

+2

고맙지 만 물론 나는 그것을 봤습니다. 그렇습니다. 수많은 방법이 있습니다. 제 질문은 어떤 방식 으로든 CGImageSourceCreateThumbnailAtIndex를 사용하여 수행 할 수 있다는 것입니다. –

0

CGImageSourceCreateThumbnailAtIndex에 제공된 옵션에서 키 kCGImageSourceCreateThumbnailWithTransform에 값 (kCFBooleanTrue)을 제공하고 전체 이미지의 방향 및 픽셀 종횡비에 따라 축소판을 회전하고 비율을 조정해야합니다.

당신은 당신이

카테고리 사용

ImageIO에서 그것을 크기를 조정할 수있는 UIImage에 추가 된 크기 대신 원하는 최대 값을 제공하기 위해이 구현을 수정할 수 있습니다이 키

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

#import <UIKit/UIKit.h> 

@interface UIImage (Resizing) 

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

@end 

UIImage + Re 크기 조정.

#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