2011-04-04 10 views
9

En mi aplicación, necesito cambiar el tamaño y recortar algunas imágenes, almacenadas localmente y en línea. Estoy usando Trevor Harmon's tutorial que implementa UIImage+Resize.iPhone CGContextRef CGBitmapContextCrear combinación de parámetros no admitida

En mi iPhone 4 (iOS 4.3.1) todo funciona bien, no tengo problemas. Pero en mi iPhone 3G (iOS 3.2) los métodos de cambio de tamaño y de recorte no funcionan para ninguna imagen (los que están almacenados localmente son PNG). Esta es la salida de la consola:

Tue Apr 5 02:34:44 Andreis-MacBook-Pro.local Puzzle[12453] <Error>: CGBitmapContextCreate:  unsupported parameter combination: 8 integer bits/component; 32 bits/pixel; 3-component color space; kCGImageAlphaLast; 288 bytes/row. 
Tue Apr 5 02:34:44 Andreis-MacBook-Pro.local Puzzle[12453] <Error>: CGBitmapContextCreate: unsupported parameter combination: 8 integer bits/component; 32 bits/pixel; 3-component color space; kCGImageAlphaLast; 288 bytes/row. 
Tue Apr 5 02:34:44 Andreis-MacBook-Pro.local Puzzle[12453] <Error>: CGBitmapContextCreate: unsupported parameter combination: 8 integer bits/component; 32 bits/pixel; 3-component color space; kCGImageAlphaLast; 288 bytes/row. 
Tue Apr 5 02:34:44 Andreis-MacBook-Pro.local Puzzle[12453] <Error>: CGBitmapContextCreate: unsupported parameter combination: 8 integer bits/component; 32 bits/pixel; 3-component color space; kCGImageAlphaLast; 288 bytes/row. 

Este es el método de cultivo

- (UIImage *)croppedImage:(CGRect)bounds 
{ 
    CGImageRef imageRef = CGImageCreateWithImageInRect([self CGImage], bounds); 
    UIImage *croppedImage = [UIImage imageWithCGImage:imageRef]; 
    CGImageRelease(imageRef); 
    return croppedImage; 
} 

El método de cambio de tamaño es la siguiente:

- (UIImage *)resizedImage:(CGSize)newSize 
      transform:(CGAffineTransform)transform 
     drawTransposed:(BOOL)transpose 
interpolationQuality:(CGInterpolationQuality)quality 
{ 
    CGRect newRect = CGRectIntegral(CGRectMake(0, 0, newSize.width, newSize.height)); 
    CGRect transposedRect = CGRectMake(0, 0, newRect.size.height, newRect.size.width); 
    CGImageRef imageRef = self.CGImage; 

    CGContextRef bitmap = CGBitmapContextCreate(NULL, 
              newRect.size.width, 
              newRect.size.height, 
              CGImageGetBitsPerComponent(imageRef), 
              0, 
              CGImageGetColorSpace(imageRef), 
              CGImageGetBitmapInfo(imageRef)); 
    if(bitmap == nil) 
     return nil; 

    CGContextConcatCTM(bitmap, transform); 

    CGContextSetInterpolationQuality(bitmap, quality); 

    CGContextDrawImage(bitmap, transpose ? transposedRect : newRect, imageRef); 

    CGImageRef newImageRef = CGBitmapContextCreateImage(bitmap); 
    UIImage *newImage = [UIImage imageWithCGImage:newImageRef]; 

    CGContextRelease(bitmap); 
    CGImageRelease(newImageRef); 

    return newImage; 
} 

Puede alguien explicarme manera que tengo este problema?

Gracias, Andrei

Respuesta

3

Ok esto puede o no puede ayudar (y aprecio que esta es una entrada antigua)

que estaba recibiendo un problema similar ya que el parámetro de ancho (en su caso newRect.size.width) tenía un componente de fracción decimal (por ejemplo, 100.001 en lugar de 100.0). Lo convertí en un número entero y viceversa, truncando el componente de decimales, y el problema desapareció. Supongo que hay una prueba para ver que el número de bits por componente x píxeles, etc. se suma, y ​​no puede tratar con puntos/píxeles fraccionarios. le invitamos a utilizar este método si ayuda.

+(CGSize) fixSize:(CGSize) forSize{ 
    NSInteger w = (NSInteger) forSize.width; 
    NSInteger h = (NSInteger) forSize.height; 
    return CGSizeMake(w, h); 
} 
+0

Estaba observando exactamente el mismo error en iOS 6. Este comentario probablemente me ahorró un viaje largo y complicado. – er0

5

Descubrí que es un problema con el espacio de color. Simplemente reemplace CGImageGetColorSpace (imageRef) con CGColorSpaceCreateDeviceRGB(). Esto funciona para mí cuando intento guardar la imagen que obtuve de AVCaptureSession. ¡Y no olvides lanzarlo!

CGColorSpaceRef rgbColorSpace = CGColorSpaceCreateDeviceRGB(); 
CGContextRef bitmap = CGBitmapContextCreate(NULL, 
              newRect.size.width, 
              newRect.size.height, 
              CGImageGetBitsPerComponent(imageRef), 
              0, 
              rgbColorSpace,//CGImageGetColorSpace(imageRef), sometimes contains unsupported colorspace 
              bitmapInfo); 
CGColorSpaceRelease(rgbColorSpace); 
3

tuve el mismo problema con iOS 5 simulador y las respuestas anteriores no resolvieron mi problema: las imágenes no fueron cargados y la consola todavía reportaron los mismos errores.

Estoy usando las categorías muy populares encontradas here.

En this blog las personas tienen los mismos problemas. La respuesta de Matt del 22 de noviembre de 2011 me ayudó.

¡Salud!

0

Estaba viendo este problema en el simulador cuando cambio de 5.1 a 6.1 para probarlo. Cerrar el simulador y abrirlo nuevamente parece haber eliminado el error.

20

Respondiendo aquí porque tenía exactamente el mismo formato de píxeles cuando recibí este error. Espero que esta respuesta ayude a alguien.

La razón por la que estaba fallando, en mi caso, fue que kCGImageAlphaLast no es un valor permitido más en iOS 8, a pesar de que funciona bien en iOS 7. El 32 PBB, la combinación de 8 bits por canal sólo permite kCGImageAlphaNoneSkip* y kCGImageAlphaPremultiplied* para el Alpha Info. Al parecer, esto siempre fue un problema, pero no se aplicó antes de iOS 8.Aquí está mi solución:

- (CGBitmapInfo)normalizeBitmapInfo:(CGBitmapInfo)oldBitmapInfo { 
    //extract the alpha info by resetting everything else 
    CGImageAlphaInfo alphaInfo = oldBitmapInfo & kCGBitmapAlphaInfoMask; 

    //Since iOS8 it's not allowed anymore to create contexts with unmultiplied Alpha info 
    if (alphaInfo == kCGImageAlphaLast) { 
     alphaInfo = kCGImageAlphaPremultipliedLast; 
    } 
    if (alphaInfo == kCGImageAlphaFirst) { 
     alphaInfo = kCGImageAlphaPremultipliedFirst; 
    } 

    //reset the bits 
    CGBitmapInfo newBitmapInfo = oldBitmapInfo & ~kCGBitmapAlphaInfoMask; 

    //set the bits to the new alphaInfo 
    newBitmapInfo |= alphaInfo; 

    return newBitmapInfo; 
} 

En mi caso la pieza en su defecto de código se veía así, donde imageRef es un CGImageRef de un PNG cargado desde el paquete de aplicación:

CGContextRef bitmap = CGBitmapContextCreate(NULL, 
               newRect.size.width, 
               newRect.size.height, 
               CGImageGetBitsPerComponent(imageRef), 
               0, 
               CGImageGetColorSpace(imageRef), 
               CGImageGetBitmapInfo(imageRef)); 

Fuentes: https://stackoverflow.com/a/19345325/3099609

https://developer.apple.com/library/mac/DOCUMENTATION/GraphicsImaging/Conceptual/drawingwithquartz2d/dq_context/dq_context.html#//apple_ref/doc/uid/TP30001066-CH203-BCIBHHBB

+2

Para cualquiera que use categorías UIImage de [tutorial de Trevor Harmon] (http://vocaro.com/trevor/blog/2009/10/12/resize-a-uiimage-the-right-way/), el código OP está usando , esta es una forma segura de arreglarlo. – Bigood

Cuestiones relacionadas