Ritagliare un uiimage da utilizzare con glktextureloader
-
12-11-2019 - |
Domanda
Sto cercando di ritagliare un'area da un uiimage da utilizzare con GlkTextureLoader. Posso impostare la trama direttamente usando l'UiImage con quanto segue:
- (void)setTextureImage:(UIImage *)image
{
NSError *error;
texture = [GLKTextureLoader textureWithCGImage:image.CGImage options:nil error:&error];
if(error)
{
NSLog(@"Texture Error:%@", error);
} else {
self.textureCoordinates[0] = GLKVector2Make(1.0f, 1.0f);
self.textureCoordinates[1] = GLKVector2Make(1.0f, 0);
self.textureCoordinates[2] = GLKVector2Make(0, 0);
self.textureCoordinates[3] = GLKVector2Make(0, 1.0f);
}
}
Tuttavia, se provo a ritagliare l'immagine in questo modo:
- (void)setTextureImage:(UIImage *)image
{
NSError *error;
CGImageRef imageRef = CGImageCreateWithImageInRect(image.CGImage, CGRectMake(0.0, 64.0f, 64.0f, 64.0f));
texture = [GLKTextureLoader textureWithCGImage:imageRef options:nil error:&error];
if(error)
{
NSLog(@"[SF] Texture Error:%@", error);
} else {
self.textureCoordinates[0] = GLKVector2Make(1.0f, 1.0f);
self.textureCoordinates[1] = GLKVector2Make(1.0f, 0);
self.textureCoordinates[2] = GLKVector2Make(0, 0);
self.textureCoordinates[3] = GLKVector2Make(0, 1.0f);
}
}
Ricevo questo errore:
Texture Error:Error Domain=GLKTextureLoaderErrorDomain Code=12 "The operation couldn’t be completed. (GLKTextureLoaderErrorDomain error 12.)" UserInfo=0x6a6b550 {GLKTextureLoaderErrorKey=Image decoding failed}
Come posso prendere una sezione di UiImage da utilizzare come trama con GlkTextureLoader?
Soluzione
Alla fine ho fatto funzionare con uiimagepngrepresentation, al momento sto imparando solo OpenGL/GLKIT - quindi non un esperto, ma immagino che il cgimage mancasse un po 'di spazio o altri dati necessari per le trame.
- (void)setTextureImage:(UIImage *)image withFrame:(CGRect)rect
{
NSError *error;
CGImageRef imageRef = CGImageCreateWithImageInRect(image.CGImage, rect);
image = [UIImage imageWithData:UIImagePNGRepresentation([UIImage imageWithCGImage:imageRef])];
texture = [GLKTextureLoader textureWithCGImage:image.CGImage options:nil error:&error];
if(error)
{
NSLog(@"[SF] Texture Error:%@", error);
} else {
self.textureCoordinates[0] = GLKVector2Make(1.0f, 1.0f);
self.textureCoordinates[1] = GLKVector2Make(1.0f, 0);
self.textureCoordinates[2] = GLKVector2Make(0, 0);
self.textureCoordinates[3] = GLKVector2Make(0, 1.0f);
}
}
Altri suggerimenti
Ho lo stesso problema. Sembra che questo bug provenga da che ho ridisegnato l'immagine.
Uscirà, se creo CGContext in questo modo:
CGBitmapContextCreate(NULL, width, height, 8, 4 * width, colorSpace, kCGBitmapByteOrderDefault|kCGImageAlphaPremultipliedLast);
Tuttavia, caricherà la trama correttamente, se creo CGContext in questo modo:
UIGraphicsBeginImageContextWithOptions(aImage.size, NO, aImage.scale);
Ho avuto lo stesso errore a causa di impostazioni alfa errate per un cgimageref. La documentazione per GlkTextureLoader elenca i formati cgimage supportati nella Tabella 1.
Ho cambiato codice utilizzato per creare contesto da
CGContextRef context = CGBitmapContextCreate(NULL, width, height, 8, width * (CGColorSpaceGetNumberOfComponents(space) + 1), space, kCGBitmapAlphaInfoMask & kCGImageAlphaPremultipliedLast);
a
CGContextRef context = CGBitmapContextCreate(NULL, width, height, 8, width * (CGColorSpaceGetNumberOfComponents(space) + 1), space, kCGBitmapAlphaInfoMask & kCGImageAlphaPremultipliedFirst);
Va tutto bene per me adesso. E questo dovrebbe risolvere il problema di Feng Stone. Per te consiglierei di saltare CGImageCreateWithImageInRect
Funziona e crea un nuovo cgContext con parametri corretti, in cui è possibile disegnare la tua immagine con CGContextDrawImage