zoukankan      html  css  js  c++  java
  • CGImageSource对图像数据读取任务的抽象

    CGImageSource对图像数据读取任务的抽象

    CGImageSource是对图像数据读取任务的抽象,通过它可以获得图像对象、缩略图、图像的属性(包括Exif信息)。

    1.创建CGImageSourceRef

    1
    2
    NSString *imagePath = [[NSBundle bundleForClass:self.class] pathForImageResource:@"test.png"];
    CGImageSourceRef imageSource = CGImageSourceCreateWithURL((__bridge CFURLRef)[NSURL fileURLWithPath:imagePath],NULL);

    2.获取图像

    1
    CGImageRef imageRef = CGImageSourceCreateImageAtIndex(imageSource, 0, NULL);

    3.创建图像的缩略图

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    //缩略图的宽和高

    double thumbnailWidth=xxx,thumbnailHeight=xxx;

    //缩略图的信息的字典

    NSDictionary *thumbnailInfo = @{

    (NSString *)kCGImageSourceCreateThumbnailFromImageAlways : @YES,

    (NSString *)kCGImageSourceThumbnailMaxPixelSize : [NSNumber numberWithInt:MAX(thumbnailWidth,thumbnailHeight)],

    (NSString *)kCGImageSourceCreateThumbnailWithTransform : @YES,

    };

    //得到缩略图

    CGImageRef imageRef = CGImageSourceCreateThumbnailAtIndex(imageSource, 0, (__bridge CFDictionaryRef)thumbnailInfo );

    4.获取图像的属性信息

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    CFDictionaryRef imageInfo = CGImageSourceCopyPropertiesAtIndex(imageSource, 0,NULL);

    //像素的宽

    NSNumber *pixelWidthObj = (__bridge NSNumber *)CFDictionaryGetValue(imageInfo, kCGImagePropertyPixelWidth);

    //像素的高

    NSNumber *pixelHeightObj = (__bridge NSNumber *)CFDictionaryGetValue(imageInfo, kCGImagePropertyPixelHeight);

    //图像的旋转方向

    NSInteger orientation = [(__bridge NSNumber *)CFDictionaryGetValue(imageInfo, kCGImagePropertyOrientation) integerValue];

    //Exif信息

    NSDictionary *exifInfo = (__bridge NSDictionary *)CFDictionaryGetValue(imageInfo, kCGImagePropertyExifAuxDictionary);

    其中获取到的kCGImagePropertyPixelHeight和kCGImagePropertyPixelHeight的数值是原始的值,也就是旋转之前的数值,所以要获取到显示图像的宽和高,需要对应kCGImagePropertyOrientation的值,而通过查看kCGImagePropertyOrientation的文档介绍,值分别从1-8,但其解释却让人看不懂,经过测试,得出与UIImageOrientation有以下的映射关系:

    UIImageOrientationUp: 1 正常方向(默认值) 如图:UIImageOrientationUp

    UIImageOrientationDown: 3 旋转180度(朝左朝右当然是一样的) 如图:UIImageOrientationDown

    UIImageOrientationLeft: 8 向左逆时针旋转90度 如图:UIImageOrientationLeft

    UIImageOrientationRight: 6 向右顺时针旋转90度 如图:UIImageOrientationRight

    UIImageOrientationUpMirrored: 2 将原图水平的翻转到背面 如图:UIImageOrientationUpMirrored

    UIImageOrientationDownMirrored: 4 在水平翻转之后再旋转180度 如图:UIImageOrientationDownMirrored

    UIImageOrientationLeftMirrored: 5 在水平翻转之后向左逆时针旋转90度 如图:UIImageOrientationLeftMirrored

    UIImageOrientationRightMirrored: 7 在水平翻译之后向右顺时针旋转90度 如图:UIImageOrientationRightMirrored

    相关的Demo:头像裁剪选择器McAvatarView

    一个计算图片高度的类方法:

    //获取网络图片的宽高

    + (CGSize)getImageSizeWithURL:(NSURL *)url {

    //    1.创建CGImageSourceRef

        CGImageSourceRef imageSource = CGImageSourceCreateWithURL((CFURLRef)url, NULL);

        CGFloat width = 0.0f, height = 0.0f;

        if (imageSource)

        {

    //        获取图像的属性信息

            CFDictionaryRef imageProperties = CGImageSourceCopyPropertiesAtIndex(imageSource, 0, NULL);

            if (imageProperties != NULL)

            {

                CFNumberRef widthNum  = CFDictionaryGetValue(imageProperties, kCGImagePropertyPixelWidth);

                if (widthNum != NULL) {

                    NSNumber *num = (__bridge NSNumber *)(widthNum);

                    width = [num floatValue];

                    //                CFNumberGetValue(widthNum, kCFNumberFloatType, &width);

                }

                

                CFNumberRef heightNum = CFDictionaryGetValue(imageProperties, kCGImagePropertyPixelHeight);

                if (heightNum != NULL) {

                    NSNumber *num = (__bridge NSNumber *)(heightNum);

                    height = [num floatValue];

                    //                CFNumberGetValue(heightNum, kCFNumberFloatType, &height);

                }

                CFRelease(imageProperties);

            }

            CFRelease(imageSource);

        }

        return CGSizeMake(width, height);

    }

    转载自:CGImageSource对图像数据读取任务的抽象   

  • 相关阅读:
    lr文件下载脚本(文件参数化重命名)
    测试部工作不受重视怎么办?
    质量管理浅谈
    测试人员职业规划
    十年软件测试经验总结
    如何管理测试项目?
    ES性能测试
    将.dat文件导入数据库
    NLPIR_Init文本分词-总是初始化失败,false,Init ICTCLAS failed!
    JavaScript-也来谈--闭包
  • 原文地址:https://www.cnblogs.com/19940122yzc/p/5698893.html
Copyright © 2011-2022 走看看