zoukankan      html  css  js  c++  java
  • IOS UIImage类方法总结

    IOS中对图片的处理  UIImage
    相信做项目时肯定会有用到 UIImage 这个类,那我们就来看一下这个类中都有什么内容。
    其实这篇文章就是在看文档的时候想记录一下文档中得方法。
    UIImage  继承于NSObject
    下面介绍一下UIImage中的方法
    首先是我们最常用的
    
    通过图片的文件名来获取这个图片
    + (UIImage *)imageNamed:(NSString *)name
    //要注意的是这个方法适用于已经导入到工程中的图片
    
    创建新图片
    
    
    
    1、+ (UIImage *)imageWithContentsOfFile:(NSString *)path
    //通过文件加载指定路径下的文件内容获得新图片
    
    2、+ (UIImage *)imageWithData:(NSData *)data
    //通过一个NSData对象来获得图片
    3、+ (UIImage *)imageWithData:(NSData *)data scale:(CGFloat)scale
    //同上,只是再加上一个图片大小比例,用来改变图片的大小
    
    4、+ (UIImage *)imageWithCGImage:(CGImageRef)cgImage
    //使用Quartz 2D对象创建UIImage
    5、+ (UIImage *)imageWithCGImage:(CGImageRef)imageRef scale:(CGFloat)scale orientation:(UIImageOrientation)orientation
    //制定图片的比例和方向,其中方向是个枚举类。
    
    6、+ (UIImage *)imageWithCIImage:(CIImage *)ciImage
    //用一个Core Image 对象创建图像
    7、+ (UIImage *)imageWithCIImage:(CIImage *)ciImage scale:(CGFloat)scale orientation:(UIImageOrientation)orientation
    //再加上比例和图片方向
    
    8、- (UIImage *)imageWithAlignmentRectInsets:(UIEdgeInsets)alignmentInsets
    //返回指定矩形区域内的图像
    
    9、+ (UIImage *)animatedImageNamed:(NSString *)name duration:(NSTimeInterval)duration
    //创建一个动态图片,动态图片持续的时间为duration
    10、+ (UIImage *)animatedImageWithImages:(NSArray *)images duration:(NSTimeInterval)duration
    //用一组图片创建一个动态图片,动态持续时间duration
    
    11、+ (UIImage *)animatedResizableImageNamed:(NSString *)name capInsets:(UIEdgeInsets)capInsets duration:(NSTimeInterval)duration
    //创建一个在可变大小的图片上指定矩形区域内的动态图片
    12、+ (UIImage *)animatedResizableImageNamed:(NSString *)name capInsets:(UIEdgeInsets)capInsets resizingMode:(UIImageResizingMode)resizingMode duration:(NSTimeInterval)duration
    //同上,只是多了一个图片变化的方式,具体来说就是平铺或者拉伸
    
    13、- (UIImage *)resizableImageWithCapInsets:(UIEdgeInsets)capInsets
    //用制定矩形区域创建图像
    14、- (UIImage *)resizableImageWithCapInsets:(UIEdgeInsets)capInsets resizingMode:(UIImageResizingMode)resizingMode
    //同上,指定图片变化方式
    
    初始化图片  
    
    
    
    方法的作用在从上面的一些方法中都能找到原型,这里就不一一注释了
    1、– initWithContentsOfFile:  //从文件加载图片
    2、– initWithData:                  //用NSData对象初始化图片
    3、– initWithData:scale:      //用NSData对象,指定的比例,初始化图片
    4、– initWithCGImage:
    5、– initWithCGImage:scale:orientation:
    6、– initWithCIImage:
    7、– initWithCIImage:scale:orientation:
    
    
    绘画图片
    
    1、– drawAtPoint:
    //在指定的点开始绘画图片,这个点就是图片的做上角顶点
    2、- (void)drawAtPoint:(CGPoint)point blendMode:(CGBlendMode)blendMode alpha:(CGFloat)alpha
    //在指定的点绘制整个图片,并使用自定义图片复合模式,并设置透明度
    
    3、– drawInRect:
    //在指定区域内绘制图片,可根据需要缩放图片
    4、– drawInRect:blendMode:alpha:
    //参照上面第二条
    5、– drawAsPatternInRect:
    //在指定区域内,平铺图片
    
    
    image的属性
      imageOrientation   //图片的方向
       size   //图片的大小size
       scale   //图片的比例
       resizingMode  //图片变化方式
       CGImage  //潜在的Quartz image
       CIImage  //潜在的Core Image
       images  //返回一个由图片组成的数组,针对于由一组图片生成的动态图片
       duration  //返回动态图片持续的时间(即动态图片播放一遍的时间)
       capInsets  //图片上选定的区域
       alignmentRectInsets  //图片平铺的区域
    
    iOS自带的提供了一个API如下
    [html] view plaincopy 
    NSData *UIImageJPEGRepresentation(UIImage *image, CGFloat compressionQuality);    
    在Iphone上有两种读取图片数据的简单方法: UIImageJPEGRepresentation和UIImagePNGRepresentation. UIImageJPEGRepresentation函数需要两个参数:图片的引用和压缩系数.而UIImagePNGRepresentation只需要图片引用作为参数.通过在实际使用过程中,比较发现: UIImagePNGRepresentation(UIImage* image) 要比UIImageJPEGRepresentation(UIImage* image, 1.0) 返回的图片数据量大很多.譬如,同样是读取摄像头拍摄的同样景色的照片, UIImagePNGRepresentation()返回的数据量大小为199K ,而 UIImageJPEGRepresentation(UIImage* image, 1.0)返回的数据量大小只为140KB,比前者少了50多KB.如果对图片的清晰度要求不高,还可以通过设置 UIImageJPEGRepresentation函数的第二个参数,大幅度降低图片数据量.譬如,刚才拍摄的图片, 通过调用UIImageJPEGRepresentation(UIImage* image, 1.0)读取数据时,返回的数据大小为140KB,但更改压缩系数后,通过调用UIImageJPEGRepresentation(UIImage* image, 0.5)读取数据时,返回的数据大小只有11KB多,大大压缩了图片的数据量 ,而且从视角角度看,图片的质量并没有明显的降低.因此,在读取图片数据内容时,建议优先使用UIImageJPEGRepresentation,并可根据自己的实际使用场景,设置压缩系数,进一步降低图片数据量大小。
    
    [html] view plaincopy 
    UIImage *imageNew = [info objectForKey:@"UIImagePickerControllerOriginalImage"];  
    imageNew = [self imageWithImage:imageNew scaledToSize:CGSizeMake(100, 100)];  
    NSData *imageData = UIImageJPEGRepresentation(imageNew, 0.0001);  
      
    m_selectImage = [UIImage imageWithData:imageData];  
    
    
    .h具体code
     
    [html] view plaincopy 
    #import <Foundation/Foundation.h>  
      
    @interface UIImage (UIImageExt)  
      
    - (UIImage *)scaleToSize:(UIImage *)img size:(CGSize)size;  
      
    - (UIImage *)imageByScalingAndCroppingForSize:(CGSize)targetSize;  
    @end  
    .m具体code
    [html] view plaincopy 
    #import "UIImageExt.h"  
      
      
    @implementation UIImage (UIImageExt)  
      
    - (UIImage *)scaleToSize:(UIImage *)img size:(CGSize)size{  
        // 创建一个bitmap的context  
        // 并把它设置成为当前正在使用的context  
        UIGraphicsBeginImageContext(size);  
        // 绘制改变大小的图片  
        [img drawInRect:CGRectMake(0, 0, size.width, size.height)];  
        // 从当前context中创建一个改变大小后的图片  
        UIImage* scaledImage = UIGraphicsGetImageFromCurrentImageContext();  
        // 使当前的context出堆栈  
        UIGraphicsEndImageContext();  
        // 返回新的改变大小后的图片  
        return scaledImage;  
    }  
      
      
      
    - (UIImage*)imageByScalingAndCroppingForSize:(CGSize)targetSize  
    {  
        UIImage *sourceImage = self;  
        UIImage *newImage = nil;  
        CGSize imageSize = sourceImage.size;  
        CGFloat width = imageSize.width;  
        CGFloat height = imageSize.height;  
        CGFloat targetWidth = targetSize.width;  
        CGFloat targetHeight = targetSize.height;  
        CGFloat scaleFactor = 0.0;  
        CGFloat scaledWidth = targetWidth;  
        CGFloat scaledHeight = targetHeight;  
        CGPoint thumbnailPoint = CGPointMake(0.0,0.0);  
          
        if (CGSizeEqualToSize(imageSize, targetSize) == NO)  
        {  
            CGFloat widthFactor = targetWidth / width;  
            CGFloat heightFactor = targetHeight / height;  
              
            if (widthFactor > heightFactor)  
                scaleFactor = widthFactor; // scale to fit height  
            else  
                scaleFactor = heightFactor; // scale to fit width  
            scaledWidth  = width * scaleFactor;  
            scaledHeight = height * scaleFactor;  
              
            // center the image  
            if (widthFactor > heightFactor)  
            {  
                thumbnailPoint.y = (targetHeight - scaledHeight) * 0.5;  
            }  
            else  
                if (widthFactor < heightFactor)  
                {  
                    thumbnailPoint.x = (targetWidth - scaledWidth) * 0.5;  
                }  
        }  
          
        UIGraphicsBeginImageContext(targetSize); // this will crop  
          
        CGRect thumbnailRect = CGRectZero;  
        thumbnailRect.origin = thumbnailPoint;  
        thumbnailRect.size.width  = scaledWidth;  
        thumbnailRect.size.height = scaledHeight;  
          
        [sourceImage drawInRect:thumbnailRect];  
          
        newImage = UIGraphicsGetImageFromCurrentImageContext();  
        if(newImage == nil)  
            NSLog(@"could not scale image");  
          
        //pop the context to get back to the default  
        UIGraphicsEndImageContext();  
        return newImage;  
    }  
      
    /** 返回一张经过处理的图片*/
    + (UIImage *)createCompressesImageWithName:(NSString *)imageName
    {
        
        UIImage *newImage = [UIImage imageWithImageSimple:[UIImage imageWithName:imageName] scaledToSize:[UIImage imageWithName:imageName].size];

        return newImage;
    }
    @end

      

  • 相关阅读:
    【crontab】误删crontab及其恢复
    New Concept English there (7)
    New Concept English there (6)
    New Concept English there (5)
    New Concept English there (4)
    New Concept English there (3)
    New Concept English there (2)Typing speed exercise
    New Concept English there (1)Typing speed exercise
    New Concept English Two 34 game over
    New Concept English Two 33 94
  • 原文地址:https://www.cnblogs.com/ndyBlog/p/3956804.html
Copyright © 2011-2022 走看看