0

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

私が知りたいのは、これらの関数をメインスレッド以外のスレッドで安全に使用できるかどうかです。

4

1 に答える 1

1

はい、これらの関数はスレッドセーフです。

ドキュメンテーション

iOS 4 以降では、アプリの任意のスレッドからこの関数を呼び出すことができます。

于 2012-12-18T19:58:14.073 に答える