[英]Convert instance of a parent class into its subclass
I've subclassed UIImage to create UIImageExtra. 我将UIImage子类化以创建UIImageExtra。 I have a method within UIImageExtra called adjustForResolution that adjusts the size of the UIImage.
我在UIImageExtra中有一个方法称为AdjustForResolution,用于调整UIImage的大小。 However I want it to return a UIImageExtra.
但是我希望它返回UIImageExtra。 I understand that I need to convert it but not exactly sure how.
我了解我需要对其进行转换,但不确定如何进行转换。 Basically, if I resize a UIImageExtra it becomes a UIImage.
基本上,如果我调整UIImageExtra的大小,它将变成UIImage。
Here is my UIImageExtra class: 这是我的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
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的类扩展以处理调整大小,然后创建一个从NSObject继承来容纳两个UIImage的新类是否会更容易。一个是原始UIImage,另一个是调整大小的副本?
UIImage+Extra.h: UIImage + Extra.h:
#import <UIKit/UIKit.h>
@interface UIImage (Extra)
-(void)adjustForResolution;
-(void)imageByScalingAndCroppingForSize:(CGSize)targetSize;
@end
UIImage+Extra.m: 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: 然后在您的自定义NSObject类上,您的init可能类似于:
#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 刚刚键入,所以可能会有一些错别字,但我希望对您有所帮助
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. 不使用contensOfFile而不是initWithCGImage的缺点是无法清除生成的图像,因此迟早会遇到内存问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.