zoukankan      html  css  js  c++  java
  • 关于图片处理的方法整理

    之前对于图片的概念很模糊,一直以为直接拿来用就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;

    }

  • 相关阅读:
    C# 实现list=list.OrderBy(q=>q.字段名).ToList(); 按多个字段排序
    c# dev gridcontrol 焦点行失去焦点有背景颜色
    c# dev gridcontrol format rule的使用
    鼠标模拟左键单击
    IDEA快捷键/本文仅供自己参考使用如有侵权立删
    Git学习笔记
    bootstrap帮助文档
    bootstrap笔记
    关于Action模型驱动无法获取属性的问题
    SSH整合hibernate无法正常自动生成表
  • 原文地址:https://www.cnblogs.com/alihaiseyao/p/3544444.html
Copyright © 2011-2022 走看看