CoreGraphics Image resize

前端 未结 2 1114
情深已故
情深已故 2021-01-14 09:05

This code is from Apple\'s WWDC 2011 Session 318 - iOS Performance in Depth and uses CoreGraphics to create thumbnails from server hosted images.

CGImageSour         


        
相关标签:
2条回答
  • 2021-01-14 09:35

    Try image resize:

    -(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;
    }
    

    i use it in my code for awhile but i can't remember it's source

    try this also resizing a UIImage without loading it entirely into memory?

    0 讨论(0)
  • 2021-01-14 09:43

    School boy mistake.

    Didn't add #import <ImageIO/ImageIO.h>

    0 讨论(0)
提交回复
热议问题