iPhone : App의 문서의 파일에서 이미지를로드 할 때 Texture2d 생성이 훨씬 느려집니다.
문제
무엇보다 느립니다. 앱 번들에서로드 된 이미지에서 동일한 텍스처를 만드는 것보다 느리게. 얼마나 느리게? iPhone에서 80 배 느리게, Mac의 비슷한 비율 (그러나 전체적으로 더 빠릅니다).
아래의 예제는 ImageNamed가있는 이미지를로드하는 것을 보여줍니다.; 첫 번째 이미지에서 텍스처 생성; 앱의 문서 디렉토리의 파일에 이미지 저장; 해당 파일에서 이미지를로드; 두 번째 이미지에서 텍스처 생성.
이미지는 640x640 PNG, 100 텍스처 64x64는 각 경우에 생성됩니다. 생성 시간은 0.51 초 대 41.3 초입니다.
누구 든지이 큰 차이를 설명하고 가능한 경우 첫 번째만큼 빠르게 만들기 위해 두 번째 케이스 속도를 높이는 방법과 수단을 지적 할 수 있습니까?
루 디프
#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];
}
}
해결책
프로젝트를 구축 할 때 Xcode는 리소스를 넣은 PNG를 최적화합니다.
이 기사는 자세히 설명합니다. http://iphonedevelopment.blogspot.com/2008/10/iphone-optimized-pngs.html
제휴하지 않습니다 StackOverflow