I’ve subclassed UIImage to create UIImageExtra. I have a method within UIImageExtra called adjustForResolution that adjusts the size of the UIImage. However I want it to return a UIImageExtra. I understand that I need to convert it but not exactly sure how. Basically, if I resize a UIImageExtra it becomes a UIImage.
Here is my UIImageExtra class:
#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
Would it be easier to make a class extension of UIImage to handle resizing then create a new class what inherits from NSObject to hold two UIImages.. One being the original UIImage and the other the resized copy?
UIImage+Extra.h:
UIImage+Extra.m:
Then on your custom NSObject class your init could so something like:
Just typed that so there may be some typos but I hope it helps