转换一个父类的实例到它的子类(Convert instance of a parent class

2019-10-16 15:46发布

我子类的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

Answer 1:

会是更容易做出的UIImage的类扩展到处理调整然后创建一个新的类从NSObject的继承什么,以保持两个UIImages ..一个是原来的UIImage和其他调整大小副本?

的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

然后在您的自定义NSObject类在你的初始化会那么这样的:

#import "UIImage+Extra"

...

//initialisation 

if (self) {

     originalImage = [UIImage imageNamed:@"theimage.png"];
     resizedImage = [UIImage imageNamed:@"theimage.png"];
     [resizedImage adjustForResolution];

}

刚刚输入,如此可能会有一些拼写错误,但我希望它能帮助



Answer 2:

我认为这是你想要做什么:

- (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;
}

不使用contensOfFile代替initWithCGImage的缺点是,所得到的图像不是吹出的,那么你迟早会得到内存问题。



文章来源: Convert instance of a parent class into its subclass