iPhone: la creación de Texture2D mucho más lento cuando la imagen cargada desde el archivo de documentos de aplicaciones, ¿por qué?

StackOverflow https://stackoverflow.com/questions/1850383

Pregunta

Más lento que lo que? más lento que la creación de las mismas texturas de una imagen cargada desde el paquete de aplicación. ¿Cuánto más lento? 80 veces más lento en el iPhone, proporción similar (pero más rápido en general) en Mac.

Mi ejemplo a continuación muestra la carga una imagen con imageNamed:; la creación de texturas de la primera imagen; guardar la imagen en un archivo en el directorio de documentos de la aplicación; cargar una imagen de ese archivo; la creación de texturas de la segunda imagen.

Las imágenes son PNG 640x640, 64x64 100 texturas se crean en cada caso. horas de creación son 0,51 s frente a 41,3 s.

Puede alguien explicar esta diferencia enorme, y me apunte a los medios de acelerar el segundo caso, para que sea más rápido que el primero, si es posible?

Rudif

#import "Texture2D.h"

#define START_TIMER NSTimeInterval start = [NSDate timeIntervalSinceReferenceDate];
#define END_TIMER NSTimeInterval stop = [NSDate timeIntervalSinceReferenceDate]; NSLog(@"Time = %f", stop-start); 


@interface UIImage (CS_Extensions)
-(UIImage *) imageAtRect:(CGRect)rect;
+(NSString *) documentsDirectory;
+(void) saveImage:(UIImage *)image toDocumentsFile:(NSString *)filename;
+(UIImage *) imageFromDocumentsFile:(NSString *)filename;
+(BOOL) documentsFileExists:(NSString *)filename;
+(void) createTexturesFromImage:(UIImage *)image640x640 texture:(Texture2D **)texture;

@end;

@implementation UIImage (MiscExt)

-(UIImage *)imageAtRect:(CGRect)rect {
    CGImageRef imageRef = CGImageCreateWithImageInRect([self CGImage], rect);
    UIImage* subImage = [UIImage imageWithCGImage: imageRef];
    CGImageRelease(imageRef);
    return subImage;
}

+(NSString *) documentsDirectory {
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    return documentsDirectory;
}

+(UIImage *) imageFromDocumentsFile:(NSString *)filename {
    //  NSString *documentsDirectory = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
    NSString *documentsDirectory = [self documentsDirectory];
    NSString *path = [NSString stringWithFormat:@"%@/%@", documentsDirectory, filename];
    NSLog(@"%s : path %@", __FUNCTION__, path);
    NSData *data = [[NSData alloc] initWithContentsOfFile:path];
    UIImage *image = [[UIImage alloc] initWithData:data];
    return image;
}

+(void) saveImage:(UIImage *)image toDocumentsFile:(NSString *)filename {
    if (image != nil) {     // save to local file
        //      NSString *documentsDirectory = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
        NSString *documentsDirectory = [self documentsDirectory];
        NSString *path = [NSString stringWithFormat:@"%@/%@", documentsDirectory, filename];
        NSLog(@"%s : path %@", __FUNCTION__, path);
        //You can write an NSData to the fs w/ a method on NSData.
        //If you have a UIImage, you can do UIImageJPEGRepresentation() or UIImagePNGRepresentation to get data.
        NSData *data = UIImagePNGRepresentation(image);
        [data writeToFile:path atomically:YES];
        // Check if file exists
        NSFileManager *fileManager = [NSFileManager defaultManager];
        BOOL ok = [fileManager fileExistsAtPath:path];
        if (ok) {
            NSLog(@"%s : written file %@", __FUNCTION__, path);
        }
        else {
            NSLog(@"%s : failed to write file %@", __FUNCTION__, path);
        }
    }
}

+(BOOL) documentsFileExists:(NSString *)filename {
    NSString *documentsDirectory = [self documentsDirectory];
    NSString *path = [documentsDirectory stringByAppendingPathComponent:filename];
    NSLog(@"%s : path %@", __FUNCTION__, path);
    BOOL exists = [[NSFileManager defaultManager] fileExistsAtPath:path];
    return exists;
}

+(void) createTexturesFromImage:(UIImage *)image640x640 texture:(Texture2D **)texture {
    NSLog(@"%s -> ", __FUNCTION__);
    START_TIMER;
    for (int x = 0; x < 9; ++x) {
        for (int y = 0; y < 9; ++y) {
            UIImage *ulCorner = [image640x640 imageAtRect:CGRectMake(x*64,y*64,64,64)];
            texture[y*10+x] = [[Texture2D alloc] initWithImage:ulCorner];
        }
    }
    END_TIMER;
    NSLog(@"%s <- ", __FUNCTION__);
}

@end


-(void) test {

    Texture2D *texture1[100];
    Texture2D *texture2[100];

    // compare texture creation from a bundled file vs Documents file
    {
        UIImage *imageBundled = [UIImage imageNamed:@"bivio-640x640.png"];
        [UIImage createTexturesFromImage:imageBundled texture:texture1];

        [UIImage saveImage:imageBundled toDocumentsFile:@"docfile.png"];
        BOOL ok = [UIImage documentsFileExists:@"docfile.png"];

        UIImage *imageFromFile = [UIImage imageFromDocumentsFile:@"docfile.png"];
        [UIImage createTexturesFromImage:imageFromFile texture:texture2];
    }
}
¿Fue útil?

Solución

Cuando se construyó su proyecto, XCode optimiza PNG que se pone en recursos.

En este artículo se explica en detalles: http: // iphonedevelopment. blogspot.com/2008/10/iphone-optimized-pngs.html

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top