本文介绍了将父类的实例转换为其子类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经将 UIImage 子类化来创建 UIImageExtra.我在 UIImageExtra 中有一个名为 adjustForResolution 的方法,可以调整 UIImage 的大小.但是我希望它返回一个 UIImageExtra.我知道我需要转换它,但不确定如何转换.基本上,如果我调整 UIImageExtra 的大小,它就会变成 UIImage.

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.

这是我的 UIImageExtra 类:

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

推荐答案

制作 UIImage 的类扩展来处理调整大小,然后创建一个继承自 NSObject 的新类来保存两个 UIImages 会更容易吗..一个是原始的UIImage 和另一个调整大小的副本?

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.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

然后在您的自定义 NSObject 类上,您的 init 可能类似于:

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

这篇关于将父类的实例转换为其子类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-15 16:52