objective-c - 将父类的实例转换为其子类

标签 objective-c ios xcode uiimage subclassing

我将 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

最佳答案

创建一个 UIImage 的类扩展来处理调整大小然后创建一个继承自 NSObject 的新类来保存两个 UIImage 是否更容易。一个是原始 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 类上,您的 init 可以这样:

#import "UIImage+Extra"

...

//initialisation 

if (self) {

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

}

刚刚打出来的,可能有一些错别字,希望对你有帮助

关于objective-c - 将父类的实例转换为其子类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10421031/

相关文章:

ios - 如何在 Swift/iOS 中解析 Json 并获取所需的值?

ios - 有没有办法在 Interface Builder 中预加载 WKWebView?

iphone - 可以使用 popViewController Animated 传递数据吗?

ios - 如何在 Swift 中向下转换/转换结构的泛型类型

ios - 在 iOS 中将 NSInlineData 转换为 Nstring

ios - Async NSURLConnection 触发其他 Async NSURLConnection : what is the best way of doing this?

objective-c - 在执行需要在主线程上执行的大量操作时,避免阻塞 UI

objective-c - 在透明 NSWindow 中隐藏不透明 NSView 留下阴影

objective-c - 如何在 map 工具包中显示默认用户位置和自定义注释 View ?

swift - 为什么array的append方法不能在viewController中使用?