2012-12-18 6 views
0

내가 가지고있는 UIImage 객체의 크기를 조정하기위한 다음과 같은 카테고리 :있는 UIImage 크기 조정

// UIImage+TSResize.h 

#import <UIKit/UIKit.h> 

/** 
    This is a UIImage category that allows for the resizing of UIImages. The code for the main function (imageWithImage:scaledToFillSize:withScaleFactor: was obtained at: http://stackoverflow.com/questions/2658738/the-simplest-way-to-resize-an-uiimage 
*/ 
@interface UIImage (TSResize) 

/** 
    Returns a resized UIImage that will fill the area provided. 
    @param image The image to resize. 
    @param size The size for the new image. 
    @param scaleFactor The scale factor to apply to the image. This is 2.0 for retina display device. If 0.0 is specfied, it will select the scale factor for the current device. 
    @returns The resized image. 
*/ 
+ (UIImage *)imageWithImage:(UIImage *)image scaleToFillSize:(CGSize)size withScaleFactor:(CGFloat)scaleFactor; 

/** 
    Returns a aspect resized UIImage that will fit within the size provided. 
    @param image The image to resize. 
    @param size The size that the new image should fit within. 
    @param scaleFactor The scale factor to apply to the image. This is 2.0 for retina display device. If 0.0 is specfied, it will select the scale factor for the current device. 
    @returns The resized image. 
*/ 
+ (UIImage *)imageWithImage:(UIImage *)image scaleAspectFitSize:(CGSize)size withScaleFactor:(CGFloat)scaleFactor; 

/** 
    Returns a aspect resized UIImage that will fill size provided. This image has not been cropped to remain within the size provided if the aspect ratios are different. 
    @param image The image to resize. 
    @param size The size that the image should fill. 
    @param scaleFactor The scale factor to apply to the image. This is 2.0 for retina display device. If 0.0 is specfied, it will select the scale factor for the current device. 
    @returns The resized image. 
*/ 
+ (UIImage *)imageWithImage:(UIImage *)image scaleToAspectFillSizeWithoutCropping:(CGSize)size withScaleFactor:(CGFloat)scaleFactor; 

@end 

&

// UIImage+Resize.m 

#import "UIImage+TSResize.h" 

@implementation UIImage (TSResize) 

+ (UIImage *)imageWithImage:(UIImage *)image scaleToFillSize:(CGSize)size withScaleFactor:(CGFloat)scaleFactor { 
    UIGraphicsBeginImageContextWithOptions(size, NO, scaleFactor); 
    [image drawInRect:CGRectMake(0, 0, size.width, size.height)]; 
    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext(); 
    UIGraphicsEndImageContext(); 
    return newImage; 
} 

+ (UIImage *)imageWithImage:(UIImage *)image scaleAspectFitSize:(CGSize)size withScaleFactor:(CGFloat)scaleFactor { 
    // Find the smallest scaling factor of the two sides. 
    CGFloat scalingFactor = 0.0; 

    CGFloat heightScalingFactor = size.height/image.size.height; 
    CGFloat widthScalingFactor = size.width/image.size.width; 

    if (heightScalingFactor < widthScalingFactor) { 
     scalingFactor = heightScalingFactor; 
    } else { 
     scalingFactor = widthScalingFactor; 
    } 

    CGSize scaledSize; 

    scaledSize.height = image.size.height * scalingFactor; 
    scaledSize.width = image.size.width * scalingFactor; 

    return [self imageWithImage:image scaleToFillSize:scaledSize withScaleFactor:scaleFactor]; 
} 

+ (UIImage *)imageWithImage:(UIImage *)image scaleToAspectFillSizeWithoutCropping:(CGSize)size withScaleFactor:(CGFloat)scaleFactor { 
    // Find the largest scaling factor of the two sides. 
    CGFloat scalingFactor = 0.0; 

    CGFloat heightScalingFactor = size.height/image.size.height; 
    CGFloat widthScalingFactor = size.width/image.size.width; 

    if (heightScalingFactor > widthScalingFactor) { 
     scalingFactor = heightScalingFactor; 
    } else { 
     scalingFactor = widthScalingFactor; 
    } 

    CGSize scaledSize; 

    scaledSize.height = image.size.height * scalingFactor; 
    scaledSize.width = image.size.width * scalingFactor; 

    return [self imageWithImage:image scaleToFillSize:scaledSize withScaleFactor:scaleFactor]; 
} 

@end 

내가 알고 싶은 경우는 다른 스레드에서 이러한 기능을 사용하는 것이 안전 다음 주 스레드.

답변

1

예 이러한 함수는 스레드로부터 안전합니다.

Documentation

아이폰 OS 4에서

이후, 당신은 응용 프로그램의 모든 스레드에서이 함수를 호출 할 수 있습니다.

+0

지금까지 작동하는 것 같습니다. 이제는 스레드 별 크기 조정과 관련된 메모리 문제를 해결해야합니다. – robhasacamera

관련 문제