我有以下类别用于调整 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
我想知道的是在主线程以外的线程中使用这些函数是否安全。