关于图片处理的方法整理

之前对于图片的概念很模糊,一直以为直接拿来用就ok啦,最近项目中发现原来没那么简单,需要处理一下,比如各种类型的图片大小就不一样,像素也不尽相同,所有一般拿到图片对他进行处理之后使用,往往可以节省不少资源,这对于开发手机应用来说帮助不少;

把图片先转换成位图在使用:

+ (UIImage *)decodedImageWithImage:(UIImage *)image

{

    if (image.images)

    {

        // Do not decode animated images

        return image;

    }

    

    CGImageRef imageRef = image.CGImage;

    CGSize imageSize = CGSizeMake(CGImageGetWidth(imageRef), CGImageGetHeight(imageRef));

    CGRect imageRect = (CGRect){.origin = CGPointZero, .size = imageSize};

    

    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();

    CGBitmapInfo bitmapInfo = CGImageGetBitmapInfo(imageRef);

    

    int infoMask = (bitmapInfo & kCGBitmapAlphaInfoMask);

    BOOL anyNonAlpha = (infoMask == kCGImageAlphaNone ||

                        infoMask == kCGImageAlphaNoneSkipFirst ||

                        infoMask == kCGImageAlphaNoneSkipLast);

    

    // CGBitmapContextCreate doesn't support kCGImageAlphaNone with RGB.

    // https://developer.apple.com/library/mac/#qa/qa1037/_index.html

    if (infoMask == kCGImageAlphaNone && CGColorSpaceGetNumberOfComponents(colorSpace) > 1)

    {

        // Unset the old alpha info.

        bitmapInfo &= ~kCGBitmapAlphaInfoMask;

        

        // Set noneSkipFirst.

        bitmapInfo |= kCGImageAlphaNoneSkipFirst;

    }

    // Some PNGs tell us they have alpha but only 3 components. Odd.

    else if (!anyNonAlpha && CGColorSpaceGetNumberOfComponents(colorSpace) == 3)

    {

        // Unset the old alpha info.

        bitmapInfo &= ~kCGBitmapAlphaInfoMask;

        bitmapInfo |= kCGImageAlphaPremultipliedFirst;

    }

    

    // It calculates the bytes-per-row based on the bitsPerComponent and width arguments.

    CGContextRef context = CGBitmapContextCreate(NULL,

                                                 imageSize.width,

                                                 imageSize.height,

                                                 CGImageGetBitsPerComponent(imageRef),

                                                 0,

                                                 colorSpace,

                                                 bitmapInfo);

    CGColorSpaceRelease(colorSpace);

    

    // If failed, return undecompressed image

    if (!context) return image;

    CGContextDrawImage(context, imageRect, imageRef);

    CGImageRef decompressedImageRef = CGBitmapContextCreateImage(context);

    CGContextRelease(context);

    UIImage *decompressedImage = [UIImage imageWithCGImage:decompressedImageRef scale:image.scale orientation:image.imageOrientation];

    CGImageRelease(decompressedImageRef);

    return decompressedImage;

}

把图片转换成特地大小的位图在使用:

+ (UIImage *)decodedImageWithImage:(UIImage *)image withSize:(CGSize)transSize

{

    if (image.images)

    {

        // Do not decode animated images

        return image;

    }

    

    CGImageRef imageRef = image.CGImage;

    CGSize imageSize = transSize;//CGSizeMake(CGImageGetWidth(imageRef), CGImageGetHeight(imageRef));

    

    CGRect imageRect = (CGRect){.origin = CGPointZero, .size = imageSize};

    

    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();

    CGBitmapInfo bitmapInfo = CGImageGetBitmapInfo(imageRef);

    

    int infoMask = (bitmapInfo & kCGBitmapAlphaInfoMask);

    BOOL anyNonAlpha = (infoMask == kCGImageAlphaNone ||

                        infoMask == kCGImageAlphaNoneSkipFirst ||

                        infoMask == kCGImageAlphaNoneSkipLast);

    

    // CGBitmapContextCreate doesn't support kCGImageAlphaNone with RGB.

    // https://developer.apple.com/library/mac/#qa/qa1037/_index.html

    if (infoMask == kCGImageAlphaNone && CGColorSpaceGetNumberOfComponents(colorSpace) > 1)

    {

        // Unset the old alpha info.

        bitmapInfo &= ~kCGBitmapAlphaInfoMask;

        

        // Set noneSkipFirst.

        bitmapInfo |= kCGImageAlphaNoneSkipFirst;

    }

    // Some PNGs tell us they have alpha but only 3 components. Odd.

    else if (!anyNonAlpha && CGColorSpaceGetNumberOfComponents(colorSpace) == 3)

    {

        // Unset the old alpha info.

        bitmapInfo &= ~kCGBitmapAlphaInfoMask;

        bitmapInfo |= kCGImageAlphaPremultipliedFirst;

    }

    

    // It calculates the bytes-per-row based on the bitsPerComponent and width arguments.

    CGContextRef context = CGBitmapContextCreate(NULL,

                                                 imageSize.width,

                                                 imageSize.height,

                                                 CGImageGetBitsPerComponent(imageRef),

                                                 0,

                                                 colorSpace,

                                                 bitmapInfo);

    CGColorSpaceRelease(colorSpace);

    

    // If failed, return undecompressed image

    if (!context) return image;

    CGContextDrawImage(context, imageRect, imageRef);

    CGImageRef decompressedImageRef = CGBitmapContextCreateImage(context);

    CGContextRelease(context);

    UIImage *decompressedImage = [UIImage imageWithCGImage:decompressedImageRef scale:image.scale orientation:image.imageOrientation];

    CGImageRelease(decompressedImageRef);

    return decompressedImage;

}

原文地址:https://www.cnblogs.com/alihaiseyao/p/3544444.html