Skip to content

Instantly share code, notes, and snippets.

@Shilo
Last active December 22, 2015 06:49
Show Gist options
  • Select an option

  • Save Shilo/6433502 to your computer and use it in GitHub Desktop.

Select an option

Save Shilo/6433502 to your computer and use it in GitHub Desktop.
A SPTexture category to handle GLKTextureLoaderErrorDataPreprocessingFailure error.
//
// SPTexture+PNGErrorHandler.h
//
// Created by Shilo White on 9/3/13.
//
//
#import "SPTexture.h"
@interface SPTexture (PNGErrorHandler)
@end
//
// SPTexture+PNGErrorHandler.m
// Sparrow2Framework
//
// Created by Shilo White on 9/3/13.
//
//
#import "SPTexture+PNGErrorHandler.h"
@interface SPTexture ()
+ (NSDictionary *)optionsForPath:(NSString *)path mipmaps:(BOOL)mipmaps pma:(BOOL)pma;
+ (BOOL)isCompressedFile:(NSString *)path;
@end
@implementation SPTexture (PNGErrorHandler)
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wobjc-protocol-method-implementation"
- (id)initWithContentsOfFile:(NSString *)path generateMipmaps:(BOOL)mipmaps
premultipliedAlpha:(BOOL)pma
{
NSString *fullPath = [SPUtils absolutePathToFile:path];
if (!fullPath)
[NSException raise:SP_EXC_FILE_NOT_FOUND format:@"File '%@' not found", path];
NSError *error = NULL;
NSData *data = [NSData dataWithUncompressedContentsOfFile:fullPath];
NSDictionary *options = [SPTexture optionsForPath:path mipmaps:mipmaps pma:pma];
GLKTextureInfo *info = [GLKTextureLoader textureWithContentsOfData:data
options:options error:&error];
if (!info)
{
if (error.code == 12)
{
//Error domain: GLKTextureLoaderErrorDataPreprocessingFailure
//Error description: The data could not be preprocessed correctly.
NSLog(@"Error loading texture \"%@\", will use UIImage.", fullPath);
return [self initWithContentsOfImage:[UIImage imageWithContentsOfFile:fullPath]];
}
[NSException raise:SP_EXC_FILE_INVALID
format:@"Error loading texture: %@", [error localizedDescription]];
return nil;
}
else if (mipmaps && (![SPUtils isPowerOfTwo:info.width] || ![SPUtils isPowerOfTwo:info.height])
&& glGetError() == GL_INVALID_OPERATION)
{
[NSException raise:SP_EXC_INVALID_OPERATION
format:@"Mipmapping is only supported for textures with sidelengths that "
@"are powers of two."];
}
return [[SPGLTexture alloc] initWithTextureInfo:info scale:[fullPath contentScaleFactor]
premultipliedAlpha:pma];
}
+ (void)loadFromFile:(NSString *)path generateMipmaps:(BOOL)mipmaps premultipliedAlpha:(BOOL)pma
onComplete:(SPTextureLoadingBlock)callback;
{
NSString *fullPath = [SPUtils absolutePathToFile:path];
float actualScaleFactor = [fullPath contentScaleFactor];
if ([self isCompressedFile:path])
[NSException raise:SP_EXC_INVALID_OPERATION
format:@"Async loading of gzip-compressed files is not supported"];
if (!fullPath)
[NSException raise:SP_EXC_FILE_NOT_FOUND format:@"File '%@' not found", path];
NSDictionary *options = [SPTexture optionsForPath:path mipmaps:mipmaps pma:pma];
EAGLSharegroup *sharegroup = Sparrow.currentController.context.sharegroup;
GLKTextureLoader *loader = [[GLKTextureLoader alloc] initWithSharegroup:sharegroup];
[loader textureWithContentsOfFile:fullPath options:options queue:NULL
completionHandler:^(GLKTextureInfo *info, NSError *outError)
{
SPTexture *texture = nil;
if (!outError)
texture = [[SPGLTexture alloc] initWithTextureInfo:info scale:actualScaleFactor
premultipliedAlpha:pma];
if (outError.code == 12)
{
//Error domain: GLKTextureLoaderErrorDataPreprocessingFailure
//Error description: The data could not be preprocessed correctly.
NSLog(@"Error loading texture \"%@\", will use UIImage.", fullPath);
[NSURLConnection sendAsynchronousRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:fullPath]] queue:[NSOperationQueue mainQueue]
completionHandler:^(NSURLResponse *response, NSData *data, NSError *outError)
{
SPTexture *texture = nil;
if (!outError)
texture = [[SPTexture alloc] initWithContentsOfImage:[UIImage imageWithData:data scale:actualScaleFactor] generateMipmaps:mipmaps];
callback(texture, outError);
}];
return;
}
callback(texture, outError);
}];
}
+ (void)loadFromURL:(NSURL *)url generateMipmaps:(BOOL)mipmaps scale:(float)scale
onComplete:(SPTextureLoadingBlock)callback
{
if ([self isCompressedFile:url.path])
[NSException raise:SP_EXC_INVALID_OPERATION
format:@"Async loading of gzip-compressed files is not supported"];
NSDictionary *options = @{ GLKTextureLoaderGenerateMipmaps: @(mipmaps) };
EAGLSharegroup *sharegroup = Sparrow.currentController.context.sharegroup;
GLKTextureLoader *loader = [[GLKTextureLoader alloc] initWithSharegroup:sharegroup];
[loader textureWithContentsOfURL:url options:options queue:NULL
completionHandler:^(GLKTextureInfo *info, NSError *outError)
{
SPTexture *texture = nil;
if (!outError)
texture = [[SPGLTexture alloc] initWithTextureInfo:info scale:scale];
if (outError.code == 12)
{
//Error domain: GLKTextureLoaderErrorDataPreprocessingFailure
//Error description: The data could not be preprocessed correctly.
NSLog(@"Error loading texture \"%@\", will use UIImage.", url.absoluteString);
[NSURLConnection sendAsynchronousRequest:[NSURLRequest requestWithURL:url] queue:[NSOperationQueue mainQueue]
completionHandler:^(NSURLResponse *response, NSData *data, NSError *outError)
{
SPTexture *texture = nil;
if (!outError)
texture = [[SPTexture alloc] initWithContentsOfImage:[UIImage imageWithData:data scale:scale] generateMipmaps:mipmaps];
callback(texture, outError);
}];
return;
}
callback(texture, outError);
}];
}
#pragma clang diagnostic pop
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment