2009-04-29 7 views
0

J'ai un tampon qui a des données d'image JPEG. J'ai besoin d'afficher cette image dans UIImageView. J'ai besoin de convertir ce tampon image en un objet de UIImage et l'utiliser comme suitiPhone BitmapImageRep

NSData * data = [NSData dataWithContentsOfFile: appFile]; UIImage * theImage = [[UIImage alloc] initWithData: données];

Je reçois l'image affichée mais avec une faible résolution par rapport à la résolution réelle. Ai-je besoin de le convertir en bitmap, puis de l'utiliser avec UIImage? Je ne semble pas pouvoir utiliser NSBitmapImageRep. Des idées sur comment cela peut-il être réalisé?

Répondre

2

Si les dimensions du cadre UIImageView sont différentes des dimensions de l'image source, vous obtiendrez une version redimensionnée de l'image. La qualité peut être assez approximative en fonction de la quantité de conversion effectuée.

Je trouve ce code sur le net quelque part (désolé de l'auteur original - J'ai perdu l'attribution) qui effectue un redimensionnement plus lisse:

UIImage* resizedImage(UIImage *inImage, CGRect thumbRect) 
{ 
    CGImageRef   imageRef = [inImage CGImage]; 
    CGImageAlphaInfo alphaInfo = CGImageGetAlphaInfo(imageRef); 

    // There's a wierdness with kCGImageAlphaNone and CGBitmapContextCreate 
    // see Supported Pixel Formats in the Quartz 2D Programming Guide 
    // Creating a Bitmap Graphics Context section 
    // only RGB 8 bit images with alpha of kCGImageAlphaNoneSkipFirst, kCGImageAlphaNoneSkipLast, kCGImageAlphaPremultipliedFirst, 
    // and kCGImageAlphaPremultipliedLast, with a few other oddball image kinds are supported 
    // The images on input here are likely to be png or jpeg files 
    if (alphaInfo == kCGImageAlphaNone) 
     alphaInfo = kCGImageAlphaNoneSkipLast; 

    // Build a bitmap context that's the size of the thumbRect 
    CGContextRef bitmap = CGBitmapContextCreate(
               NULL, 
               thumbRect.size.width,  // width 
               thumbRect.size.height,  // height 
               CGImageGetBitsPerComponent(imageRef), // really needs to always be 8 
               4 * thumbRect.size.width, // rowbytes 
               CGImageGetColorSpace(imageRef), 
               alphaInfo 
               ); 

    // Draw into the context, this scales the image 
    CGContextDrawImage(bitmap, thumbRect, imageRef); 

    // Get an image from the context and a UIImage 
    CGImageRef ref = CGBitmapContextCreateImage(bitmap); 
    UIImage* result = [UIImage imageWithCGImage:ref]; 

    CGContextRelease(bitmap); // ok if NULL 
    CGImageRelease(ref); 

    return result; 
}