Я не совсем уверен, что правильно понял название, но я не уверен, в чем именно заключается моя проблема.
Мне нужно загрузить массив UIImages из таблицы спрайтов, которые я затем буду использовать в качестве анимации в UIImageView.
Таблица спрайтов создается с помощью TexturePacker, который генерирует огромный атлас (2048x2048) и json с описаниями спрайтов.
До сих пор он работал без проблем, даже загружая 100 кадров всего за 0,5-0,8 секунды, что меня очень устраивало.
Проблема в том, что теперь мне нужно загрузить спрайт-листы из папки «Документы» (они загружены, поэтому их нельзя интегрировать в приложение), поэтому я должен использовать UIImage imageWithContentsOfFile
вместо UIImage imagenamed
. Это увеличивает время загрузки на 5-15 секунд в зависимости от количества кадров в анимации. Я предполагаю, что проблема в том, что, поскольку изображение не кэшируется, оно загружается для каждого кадра, но я не понимаю, почему
Это код:
// With this line, perfect
//UIImage *atlas = [UIImage imageNamed:@"media/spritesheets/[email protected]"];
// But with this one, incredibly looooong loading times
UIImage *atlas = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"media/spritesheets/default-pro7@2x" ofType:@"png"]];
CGImageRef atlasCGI = [atlas CGImage];
for( NSDictionary* dict in frames) {
NSDictionary *frame = [dict objectForKey:@"frame"];
int x = [[frame objectForKey:@"x"] intValue];
int y = [[frame objectForKey:@"y"] intValue];
int width = [[frame objectForKey:@"w"] intValue];
int height = [[frame objectForKey:@"h"] intValue];
NSDictionary *spriteSize = [dict objectForKey:@"spriteSourceSize"];
int realx = [[spriteSize objectForKey:@"x"] intValue];
NSDictionary *size = [dict objectForKey:@"sourceSize"];
int realWidth = [[size objectForKey:@"w"] intValue];
int realHeight = [[size objectForKey:@"h"] intValue];
//Initialize the canvas size!
canvasSize = CGSizeMake(realWidth, realHeight);
bitmapBytesPerRow = (canvasSize.width * 4);
bitmapByteCount = (bitmapBytesPerRow * canvasSize.height);
bitmapData = malloc( bitmapByteCount );
//Create the context
context = CGBitmapContextCreate(bitmapData, canvasSize.width, canvasSize.height, 8, bitmapBytesPerRow, colorSpace, kCGImageAlphaPremultipliedLast);
// Clear background
CGContextClearRect(context,CGRectMake(0.0f,0.0f,canvasSize.width,canvasSize.height));
// Get spriteRect
CGRect cropRect = CGRectMake(x, y, width, height);
CGImageRef imageRef = CGImageCreateWithImageInRect(atlasCGI, cropRect);
//Draw the image into the bitmap context
CGContextDrawImage(context, CGRectMake(realx, 0, width, height), imageRef);
//Get the result image
resultImage = CGBitmapContextCreateImage(context);
UIImage *result = result = [UIImage imageWithCGImage:resultImage];
[images addObject:result];
//Cleanup
free(bitmapData);
CGImageRelease(resultImage);
CGImageRelease(imageRef);
}
Я даже могу попробовать загружать сеансы системного профилирования, которые показывают, где загрузка занимает так много времени.