Resize a large image before it get's into the Texture cache.
Useful if you have a 320x320 image and like to use that for iPad,Retina or SD apps too.
//
// CCSprite+TrueResize.h
//
// Truly resizes a image file. And returns the resized image to a CCSprite
// Takes into account if you already have the image loaded.
//
// Created by @hermanjakobi on 15.12.11.
//
//
#import "CCSprite.h"
@interface CCSprite (TrueResize)
+(id) spriteWithFileAndSize: (NSString*)file size:(CGSize)size;
@end
//
// CCSprite+TrueResize.m
//
// Truly resizes a image file. And returns the resized image to a CCSprite
// Takes into account if you already have the image loaded.
//
// Created by @hermanjakobi on 15.12.11.
//
//
#import "CCSprite+TrueResize.h"
#import "CCTextureCache.h"
//Get it here
//https://github.com/mattgemmell/MGImageUtilities
#import "UIImage+ProportionalFill.h"
//----------------------------------------
@implementation CCSprite (TrueResize)
+(id) spriteWithFileAndSize: (NSString*)file size:(CGSize)size
{
//test if we already have one
CCTexture2D* texture= [[CCTextureCache sharedTextureCache] textureForKey:file];
if(texture==nil){
NSString* imagePath = [[NSBundle mainBundle] pathForResource:file ofType:nil];
UIImage* img = [UIImage imageWithContentsOfFile:imagePath]; //does not cache...good for memory
UIImage* image=[ img imageToFitSize:size method:MGImageResizeCrop] ;
return [CCSprite spriteWithCGImage:image.CGImage key:file];
}else{
NSLog(@"already have texture");
return [CCSprite spriteWithTexture:texture];
}
return nil;
}
@end