UIImage的各种处理(旋转,压缩,写入,读去,计算大小)等和方法的区别的介绍

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/zhoushuangjian511/article/details/51151903

第一:UIImage的详细使用。

//

//  Image_Do_ViewController.m

//  UIImage_处理

//

//  Created by 周双建 on 16/4/12.

//  Copyright © 2016 周双建. All rights reserved.

//


#import "Image_Do_ViewController.h"

#define APP_Width     [UIScreen mainScreen].bounds.size.width

#define APP_Height    [UIScreen mainScreen].bounds.size.height

@interface Image_Do_ViewController ()

@property(nonatomic,strong) UIImageView * IM_View;

@end


@implementation Image_Do_ViewController


- (void)viewDidLoad {

    [super viewDidLoad];

    self.navigationController.navigationBarHidden = NO;

    self.navigationController.navigationBar.translucent = NO;

    self.view.backgroundColor = [[UIColor whiteColor] colorWithAlphaComponent:1.0f];

    self.title = @"Succes QQ Bar-Provide";

    [self.navigationController.navigationBar setTitleTextAttributes:@{NSFontAttributeName:[UIFont fontWithName:@"Zapfino" size:20],NSForegroundColorAttributeName:[[UIColor blueColor] colorWithAlphaComponent:0.78f]}];

#pragma ImageDO

    [self ImageLoad];

    //[self ImageLoad];

    // Do any additional setup after loading the view.

}

-(void)ImageLoad{

    self.IM_View = [[UIImageView alloc]initWithFrame:CGRectMake(20, 20, APP_Width -40, APP_Height - 104)];

    self.IM_View.contentMode = UIViewContentModeScaleAspectFit;

    /*

     *  这样加载会造成图片缓存

     *  self.IM_View.image = [UIImage imageNamed:@"zsj.jpg"];

     *

     *  1)用imageNamed的方式加载时,系统会把图像Cache到内存。如果图像比较大,或者图像比较多,用这种方式会消耗很大的内存,而且释放图像的 内存是一件相对来说比较麻烦的事情。例如:如果利用imageNamed的方式加载图像到一个动态数组NSMutableArray,然后将将数组赋予一 UIView的对象的animationImages进行逐帧动画,那么这将会很有可能造成内存泄露。并且释放图像所占据的内存也不会那么简单。但是利 imageNamed加载图像也有自己的优势。对于同一个图像系统只会把它Cache到内存一次,这对于图像的重复利用是非常有优势的。例如:你需要在 一个TableView里重复加载同样一个图标,那么用imageNamed加载图像,系统会把那个图标Cache到内存,在Table里每次利用那个图 像的时候,只会把图片指针指向同一块内存。这种情况使用imageNamed加载图像就会变得非常有效。

     */

    

    //self.IM_View.image = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle]  pathForResource:@"zsj" ofType:@"jpg"]];

    /*

     *    另一种加载方式,不进行图片的缓存

     *

     *    self.IM_View.image = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle]    pathForResource:@"zsj" ofType:@"jpg"]];


     * 仅加载图片,图像数据不会缓存。因此对于较大的图片以及使用情况较少时,那就可以用该方法,降低内存消耗。

     */

    

    /*

     *   self.IM_View.image = [UIImage imageWithData:[NSData dataWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"zsj" ofType:@"jpg"]]];

     *

     *   3) 利用NSData方式加载时,图像会被系统以数据方式加载到程序。当你不需要重用该图像,或者你需要将图像以数据方式存储到数据库,又或者你要通过网络下载一个很大的图像时,请尽量使用imageWithData的方式加载图像。

     *

     */

    /*********************************************************************************/

    NSLog(@"W:%f ----H: %f",self.IM_View.image.size.width ,self.IM_View.image.size.height);

    UIImage * MY = [UIImage imageWithData:[NSData dataWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"zsj" ofType:@"jpg"]] scale:0.5];

    NSLog(@"M_W:%f ----M_H: %f",MY.size.width ,MY.size.height);

    /*

     *

     * scale 0.5 的输出

     *

     2016-04-14 09:29:00.896 UIImage_处理[880:25088] W:500.000000 ----H: 379.000000

     2016-04-14 09:29:00.896 UIImage_处理[880:25088] M_W:1000.000000 ----M_H: 758.000000


     *

     *

     * scale 2 的输出

     2016-04-14 09:25:59.338 UIImage_处理[838:23420] W:500.000000 ----H: 379.000000

     2016-04-14 09:25:59.338 UIImage_处理[838:23420] M_W:250.000000 ----M_H: 189.500000


     */

    /*

     *+ (nullable UIImage *)imageWithData:(NSData *)data scale:(CGFloat)scale NS_AVAILABLE_IOS(6_0);

     *

     *  @scale 参数的说明

     *  scale 为小于零的数值(数值为 A)的时候 image的大小(size)是按(1/A)增加的,增加后的大小是:size * 1/A)。

     *  scale 为大于零的数值(数值为 A)的时候 image的大小(size)是按(1/A)减小的,减小后的大小是:size * 1/A)。

     */

    /*********************************************************************************/

     // 将图片转化为二进制流

    NSData * ImageData = UIImagePNGRepresentation(MY);

    /*

     *   UIImagePNGRepresentation(UIImage * __nonnull image); 

     *   return image as PNG. May return nil if image has no CGImageRef or invalid bitmap format

     */

    NSData * ImageData1 = UIImageJPEGRepresentation(MY, 1.0f);

    /*

     *  return image as JPEG. May return nil if image has no CGImageRef or invalid bitmap format. compression is 0(most)..1(least)

     */

    /*

       UIImagePNGRepresentation  |  UIImageJPEGRepresentation 两个的区别

     

       UIImageJPEGRepresentation函数需要两个参数:图片的引用和压缩系数.UIImagePNGRepresentation只需要图片引用作为参数.通过在实际使用过程中,比较发现: UIImagePNGRepresentation(UIImage* image) 要比UIImageJPEGRepresentation(UIImage* image, 1.0) 返回的图片数据量大很多.譬如,同样是读取摄像头拍摄的同样景色的照片, UIImagePNGRepresentation()返回的数据量大小为199K , UIImageJPEGRepresentation(UIImage* image, 1.0)返回的数据量大小只为140KB,比前者少了50KB.如果对图片的清晰度要求不高,还可以通过设置 UIImageJPEGRepresentation函数的第二个参数,大幅度降低图片数据量.譬如,刚才拍摄的图片, 通过调用UIImageJPEGRepresentation(UIImage* image, 1.0)读取数据时,返回的数据大小为140KB,但更改压缩系数后,通过调用UIImageJPEGRepresentation(UIImage* image, 0.5)读取数据时,返回的数据大小只有11KB,大大压缩了图片的数据量 ,而且从视角角度看,图片的质量并没有明显的降低.因此,在读取图片数据内容时,建议优先使用UIImageJPEGRepresentation,并可根据自己的实际使用场景,设置压缩系数,进一步降低图片数据量大小.

     */

    /*********************************************************************************/

    // 将图片写入文件

    NSData * ImageData_W = UIImageJPEGRepresentation(MY, 0.5f);

    // 获取系统沙河路径

    NSArray * ArrayPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);

    NSString * path = [ArrayPath objectAtIndex:0];

    // 开始写入路径的拼接

    NSString * filePath = [path stringByAppendingString:@"Image.text"];

    // 写入

    BOOL Succes =   [ImageData_W writeToFile:filePath atomically:YES];

    NSLog(@"%d",Succes);

    /*

     输出 UIImage_处理[1130:41357] 1   写入成功

     */

    // 将图片从沙河读取出来

    NSString * ReadPath = filePath;

    NSData * ReadData = [NSData dataWithContentsOfFile:ReadPath];

    UIImage * ReadImage = [UIImage imageWithData:ReadData scale:2];

    //self.IM_View.image = ReadImage;

    /*********************************************************************************/

    // 改变图像的方向

   // self.IM_View.image = [UIImage imageWithCGImage:ReadImage.CGImage scale:1.0f orientation:UIImageOrientationLeftMirrored];

    /*

     *  不用旋转控件,来实现图像的角度旋转

     typedef NS_ENUM(NSInteger, UIImageOrientation) {

     UIImageOrientationUp,            // default orientation  默认

     UIImageOrientationDown,          // 180 deg rotation     180度的转向

     UIImageOrientationLeft,          // 90 deg CCW           图像逆时针 90

     UIImageOrientationRight,         // 90 deg CW            图像顺时针 90

     UIImageOrientationUpMirrored,    // as above but image mirrored along other axis. horizontal flip                                                 图片的像素 中间对称水平翻转

     UIImageOrientationDownMirrored,  // horizontal flip      图片的像素 中间对称垂直翻转

     UIImageOrientationLeftMirrored,  // vertical flip        图片的像素 左翻转

     UIImageOrientationRightMirrored, // vertical flip        图片的像素 右翻转

     };

     */

    /*********************************************************************************/

     // 用图像获取上下文

    UIGraphicsBeginImageContext(ReadImage.size); // 创建上下文

    CGContextRef ContextRef = UIGraphicsGetCurrentContext();// 获取上下文对象

    CGContextSaveGState(ContextRef);// 保存上下文,有利于还原场景

    /*

     *  [ReadImage drawInRect:CGRectMake(10, 10, 60, 60)]; 

     *   是在指定的大小范围内绘制图片,图片可完全绘出

     */

    

    /*

     *  [ReadImage drawAtPoint:CGPointMake(100, 100)];

     *  这个是不改变图片的大小,是更改图片的起始位置

     */

    [ReadImage drawInRect:CGRectMake(10, 10, 220, 220) blendMode:kCGBlendModeSoftLight alpha:1.0f];

    /*

     *  - (void)drawInRect:(CGRect)rect blendMode:(CGBlendMode)blendMode alpha:(CGFloat)alpha;

    typedef CF_ENUM (int32_t, CGBlendMode) {

    kCGBlendModeNormal,                               正常;也是默认的模式。前景图会覆盖背景图

    kCGBlendModeMultiply,                             正片叠底;混合了前景和背景的颜色,最终颜色比原先的都暗

    kCGBlendModeScreen,                               滤色;把前景和背景图的颜色先反过来,然后混合

    kCGBlendModeOverlay,                              覆盖;能保留灰度信息,结合kCGBlendModeSaturation能保留透明度信息,在imageWithBlendMode方法中两次执行drawInRect方法实现我们基本需求

    kCGBlendModeDarken,                               变暗

    kCGBlendModeLighten,                              变亮

    kCGBlendModeColorDodge,                           颜色变淡

    kCGBlendModeColorBurn,                            颜色加深

    kCGBlendModeSoftLight,                            柔光

    kCGBlendModeHardLight,                            强光

    kCGBlendModeDifference,                           插值

    kCGBlendModeExclusion,                            排除

    kCGBlendModeHue,                                  色调

    kCGBlendModeSaturation,                           饱和度

    kCGBlendModeColor,                                颜色

    kCGBlendModeLuminosity,                           亮度

    kCGBlendModeClear,                    R = 0       清除所有的颜色

    kCGBlendModeCopy,                     R = S

    kCGBlendModeSourceIn,                 R = S*Da

    kCGBlendModeSourceOut,                R = S*(1 - Da)

    kCGBlendModeSourceAtop,               R = S*Da + D*(1 - Sa)

    kCGBlendModeDestinationOver,          R = S*(1 - Da) + D

    kCGBlendModeDestinationIn,            R = D*Sa

    kCGBlendModeDestinationOut,           R = D*(1 - Sa)

    kCGBlendModeDestinationAtop,          R = S*(1 - Da) + D*Sa     

    kCGBlendModeXOR,                      R = S*(1 - Da) + D*(1 - Sa)

    kCGBlendModePlusDarker,               R = MAX(0, (1 - D) + (1 - S))

    kCGBlendModePlusLighter               R = MIN(1, S + D)  };

*/

    //self.IM_View.image = UIGraphicsGetImageFromCurrentImageContext();

    CGContextRelease(ContextRef);// 移除保存的上下文

    UIGraphicsEndImageContext(); // 结束图像上下文

    /*********************************************************************************/

     // 图片的填充,或者拉伸

    //self.IM_View.image = [ReadImage stretchableImageWithLeftCapWidth:50 topCapHeight:50];

    /*

     *  - (UIImage *)stretchableImageWithLeftCapWidth:(NSInteger)leftCapWidth topCapHeight:(NSInteger)topCapHeight  |  - (UIImage *)resizableImageWithCapInsets:(UIEdgeInsets)capInsets

     *  这个函数是UIImage的一个实例函数,它的功能是创建一个内容可拉伸,而边角不拉伸的图片,需要两个参数,第一个是不拉伸区域和左边框的宽度,第二个参数是不拉伸区域和上边框的宽度。

     */

//    self.IM_View.image = [ReadImage resizableImageWithCapInsets:UIEdgeInsetsMake(20, 20, 202, 20)];

    self.IM_View.image = [ReadImage imageFlippedForRightToLeftLayoutDirection];


    /*********************************************************************************/

    // 计算图片的大小

    long  perMBBytes = 1024*1024;

    CGImageRef cgimage = [UIImage imageNamed:@"zsj.jpg"].CGImage;

    size_t bpp = CGImageGetBitsPerPixel(cgimage);

    size_t bpc = CGImageGetBitsPerComponent(cgimage);

    size_t bytes_per_pixel = bpp / bpc;

    long lPixelsPerMB  = perMBBytes/bytes_per_pixel;

    long totalPixel = CGImageGetWidth([UIImage imageNamed:@"zsj.jpg"].CGImage)*CGImageGetHeight([UIImage imageNamed:@"zsj.jpg"].CGImage);

    long totalFileMB = totalPixel * bpp /lPixelsPerMB;

    NSLog(@"%ld",totalFileMB);

    /*********************************************************************************/


    [self.view addSubview:self.IM_View];

}

- (void)didReceiveMemoryWarning {

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}


/*

#pragma mark - Navigation


// In a storyboard-based application, you will often want to do a little preparation before navigation

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {

    // Get the new view controller using [segue destinationViewController].

    // Pass the selected object to the new view controller.

}

*/


@end



猜你喜欢

转载自blog.csdn.net/zhoushuangjian511/article/details/51151903