1

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

2 回答 2

1

制作 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];

}

刚才打的,可能有错别字,希望对你有帮助

于 2012-05-02T20:59:36.983 回答
0

我认为这是你想要做的:

- (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 的缺点是,生成的图像不可清除,因此迟早您会遇到内存问题。

于 2012-05-03T13:10:34.700 回答