iPhone: la création d'Texture2D beaucoup plus lent lorsque l'image chargée à partir du fichier dans les documents d'application, pourquoi?

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

Question

Plus lent que quoi? plus lent que de créer les mêmes textures à partir d'une image chargée à partir du faisceau de l'application. Combien plus lent? 80 fois plus lent sur iPhone, ratio similaire (mais globalement plus rapide) sur Mac.

Mon exemple ci-dessous montre le chargement d'une image avec imageNamed:; la création de textures à partir de la première image; sauvegarde de l'image dans un fichier dans le répertoire des documents de l'application; charger une image à partir de ce fichier; la création de textures à partir de la seconde image.

Les images sont 640x640 .png, 100 textures 64x64 sont créées dans chaque cas. temps de création sont 0,51 par rapport de 41,3 s.

Quelqu'un peut-il expliquer cette énorme différence, et me pointer vers des voies et moyens d'accélérer le second cas, pour le rendre aussi vite que le premier, si possible?

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];
    }
}
Était-ce utile?

La solution

Lorsque vous avez construit votre projet, XCode permet d'optimiser PNGs que vous avez des ressources.

Cet article explique dans les détails: http: // iphonedevelopment. blogspot.com/2008/10/iphone-optimized-pngs.html

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top