• 关于图片处理的方法整理


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

    }

  • 相关阅读:
    ecshop php商城系统数据库结构及表的介绍分析
    laravel clone后需要做的操作
    十几万条数据的表中,基于帝国cms 。自己亲身体验三种批量更新数据的方法,每一种的速度是什么样的
    PHP 实现过滤参数字符的方法
    Antd Modal 可拖拽移动
    golang /js index 转换excel字母表头
    React 父组件调用子组件的方法
    React ant table 用 XLSX 导出excel文件
    css3 做出顶边倾斜的 梯形 div
    Windows10 office 点击链接提示您的组策略阻止我们为您完成此操作。设置ChromeHTML也无效.
  • 原文地址:https://www.cnblogs.com/alihaiseyao/p/3544444.html
Copyright © 2020-2023  润新知