我将 UIImage 子类化为创建 UIImageExtra。我在 UIImageExtra 中有一个名为 adjustForResolution 的方法,它可以调整 UIImage 的大小。但是我希望它返回一个 UIImageExtra。我知道我需要转换它,但不确定如何转换。基本上,如果我调整 UIImageExtra 的大小,它就会变成 UIImage。
这是我的 UIImageExtra 类:
#import "UIImageExtra.h"
#import <objc/runtime.h>
#import "UIApplication+AppDimensions.h"
#define kOriginData @"originData"
@implementation UIImageExtra
@synthesize originData;
+ (UIImageExtra *)imageNamed:(NSString *)imageName origin:(NSValue *)originData {
NSString *path = [[NSBundle mainBundle] pathForResource:imageName ofType:@""];
UIImageExtra *imageExtra = [[UIImageExtra alloc] initWithContentsOfFile:path];
imageExtra.originData = originData;
return imageExtra;
}
- (id)initWithContentsOfFile:(NSString *)path {
self = [super initWithContentsOfFile:path];
if (self) {
self = [self adjustForResolution];
}
NSLog(@"Image description: %@", [self description]);
return self;
}
- (id)adjustForResolution {
//All images are sized for ipad3 (2048 x 1536), so determine scale factor by ratio
CGSize screenSize = [UIApplication currentSize];
double scalefactor = screenSize.width/2048;
CGSize newSize = CGSizeMake(self.size.width*scalefactor, self.size.height*scalefactor);
UIImage *scaledImage = [self imageByScalingAndCroppingForSize:newSize];
return scaledImage;
}
- (id)imageByScalingAndCroppingForSize:(CGSize)targetSize
{
//returns resized image
}
@end