问题
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
回答1:
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:
#import <UIKit/UIKit.h>
@interface UIImage (Extra)
-(void)adjustForResolution;
-(void)imageByScalingAndCroppingForSize:(CGSize)targetSize;
@end
UIImage+Extra.m:
#import "UIImage+Extra.h"
@implementation UIImage (Extra)
- (void)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);
[self imageByScalingAndCroppingForSize:newSize];
}
- (id)imageByScalingAndCroppingForSize:(CGSize)targetSize
{
//instead of doing work to another object and returning it... Do it to self (UIimage)
}
@end
Then on your custom NSObject class your init could so something like:
#import "UIImage+Extra"
...
//initialisation
if (self) {
originalImage = [UIImage imageNamed:@"theimage.png"];
resizedImage = [UIImage imageNamed:@"theimage.png"];
[resizedImage adjustForResolution];
}
Just typed that so there may be some typos but I hope it helps
回答2:
I think this is what you want to do:
- (id)initWithContentsOfFile:(NSString *)path {
CGSize screenSize = [UIApplication currentSize];
double scalefactor = screenSize.width/2048;
if (scalefactor < 1) {
UIImage *img = [UIImage imageWithContentsOfFile:path];
CGSize newSize = CGSizeMake(img.size.width*scalefactor, img.size.height*scalefactor);
UIGraphicsBeginImageContext(newSize);
CGContextRef c = UIGraphicsGetCurrentContext();
[img drawInRect:(CGRect){CGPointZero, newSize}];
CGImageRef scaledImage = CGBitmapContextCreateImage(c);
UIGraphicsEndImageContext();
self = [super initWithCGImage:scaledImage];
CGImageRelease(scaledImage);
}else {
self = [super initWithContentsOfFile:path];
}
return self;
}
The disadvantage of not using contensOfFile instead of initWithCGImage is, that the resulting image is not purgeable, so sooner or later you will get memory problems.
来源:https://stackoverflow.com/questions/10421031/convert-instance-of-a-parent-class-into-its-subclass