zoukankan      html  css  js  c++  java
  • UIImage 图片处理:截图,缩放,设定大小,存储

      1 图片的处理大概分 截图(capture),  缩放(scale), 设定大小(resize),  存储(save)
      2 
      3 
      4 1.等比率缩放
      5 - (UIImage *)scaleImage:(UIImage *)image toScale:(float)scaleSize
      6 
      7 {
      8 
      9 UIGraphicsBeginImageContext(CGSizeMake(image.size.width * scaleSize, image.size.height * scaleSize);
     10 [image drawInRect:CGRectMake(0, 0, image.size.width * scaleSize, image.size.height * scaleSize)];
     11 UIImage *scaledImage = UIGraphicsGetImageFromCurrentImageContext();
     12 UIGraphicsEndImageContext();
     13 
     14 return scaledImage;
     15 
     16 }
     17 
     18 
     19 2.自定长宽
     20 - (UIImage *)reSizeImage:(UIImage *)image toSize:(CGSize)reSize
     21 
     22 {
     23 UIGraphicsBeginImageContext(CGSizeMake(reSize.width, reSize.height));
     24 [image drawInRect:CGRectMake(0, 0, reSize.width, reSize.height)];
     25 UIImage *reSizeImage = UIGraphicsGetImageFromCurrentImageContext();
     26 UIGraphicsEndImageContext();
     27 
     28 return reSizeImage;
     29 
     30 }
     31 
     32 
     33 3.处理某个特定View
     34 只要是继承UIView的object 都可以处理
     35 必须先import QuzrtzCore.framework
     36 
     37 
     38 -(UIImage*)captureView:(UIView *)theView
     39 
     40 {
     41 CGRect rect = theView.frame;
     42 UIGraphicsBeginImageContext(rect.size);
     43 CGContextRef context = UIGraphicsGetCurrentContext();
     44 [theView.layer renderInContext:context];
     45 UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
     46 UIGraphicsEndImageContext();
     47 
     48 return img;
     49 
     50 }
     51 
     52 
     53 4.储存图片
     54 储存图片这里分成储存到app的文件里和储存到手机的图片库里
     55 
     56 1) 储存到app的文件里
     57 NSString *path = [[NSHomeDirectory()stringByAppendingPathComponent:@"Documents"]stringByAppendingPathComponent:@"image.png"];
     58 [UIImagePNGRepresentation(image) writeToFile:pathatomically:YES];
     59 把要处理的图片, 以image.png名称存到app home下的Documents目录里
     60 
     61 2)储存到手机的图片库里(必须在真机使用,模拟器无法使用)
     62 CGImageRef screen = UIGetScreenImage();
     63 UIImage* image = [UIImage imageWithCGImage:screen];
     64 CGImageRelease(screen);
     65 UIImageWriteToSavedPhotosAlbum(image, self, nil, nil);
     66 UIGetScreenImage(); // 原来是private(私有)api, 用来截取整个画面,不过SDK 4.0后apple就开放了
     67 
     68 //====================================================================================
     69 
     70 以下代码用到了Quartz Framework 和 Core Graphics Framework. 在workspace的framework目录里添加这两个framework.在UIKit里,图像类UIImage和CGImageRef的画图操作都是通过Graphics Context来完成。Graphics Context封装了变换的参数,使得在不同的坐标系里操作图像非常方便。缺点就是,获取图像的数据不是那么方便。下面会给出获取数据区的代码。
     71 
     72  
     73 
     74 1. 从UIView中获取图像相当于窗口截屏。
     75 
     76 (ios提供全局的全屏截屏函数UIGetScreenView(). 如果需要特定区域的图像,可以crop一下)
     77 
     78     CGImageRef screen = UIGetScreenImage();
     79     UIImage* image = [UIImage imageWithCGImage:screen];
     80 
     81 2. 对于特定UIView的截屏。
     82 
     83 (可以把当前View的layer,输出到一个ImageContext中,然后利用这个ImageContext得到UIImage)
     84 
     85     -(UIImage*)captureView: (UIView *)theView
     86     {
     87     CGRect rect = theView.frame;
     88     UIGraphicsBeginImageContext(rect.size);
     89     CGContextRef context =UIGraphicsGetCurrentContext();
     90     [theView.layer renderInContext:context];
     91     UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
     92     UIGraphicsEndImageContext();
     93 
     94     return img;
     95     }
     96 
     97 3. 如果需要裁剪指定区域。
     98 
     99 (可以path & clip,以下例子是建一个200x200的图像上下文,再截取出左上角)
    100 
    101     UIGraphicsBeginImageContext(CGMakeSize(200,200));
    102     CGContextRefcontext=UIGraphicsGetCurrentContext();
    103     UIGraphicsPushContext(context);
    104     // ...把图写到context中,省略[indent]CGContextBeginPath();
    105     CGContextAddRect(CGMakeRect(0,0,100,100));
    106     CGContextClosePath();[/indent]CGContextDrawPath();
    107     CGContextFlush(); // 强制执行上面定义的操作
    108     UIImage* image = UIGraphicGetImageFromCurrentImageContext();
    109     UIGraphicsPopContext();
    110 
    111 4. 存储图像。
    112 
    113 (分别存储到home目录文件和图片库文件。)
    114 
    115 存储到目录文件是这样
    116 
    117     NSString *path = [[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"] stringByAppendingPathComponent:@"image.png"];
    118     [UIImagePNGRepresentation(image) writeToFile:path atomically:YES];
    119 
    120 若要存储到图片库里面
    121 
    122     UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil);
    123 
    124 
    125 5.  互相转换UImage和CGImage。
    126 
    127 (UImage封装了CGImage, 互相转换很容易)
    128 
    129     UIImage* imUI=nil;
    130     CGImageRef imCG=nil;
    131     imUI = [UIImage initWithCGImage:imCG];
    132     imCG = imUI.CGImage;
    133 
    134 6. 从CGImage上获取图像数据区。
    135 
    136 (在apple dev上有QA, 不过好像还不支持ios)
    137 
    138 
    139 下面给出一个在ios上反色的例子
    140 
    141     -(id)invertContrast:(UIImage*)img
    142     {
    143     CGImageRef inImage = img.CGImage; 
    144     CGContextRef ctx;
    145     CFDataRef m_DataRef;
    146     m_DataRef = CGDataProviderCopyData(CGImageGetDataProvider(inImage)); 
    147 
    148     int width = CGImageGetWidth( inImage );
    149     int height = CGImageGetHeight( inImage );
    150 
    151     int bpc = CGImageGetBitsPerComponent(inImage);
    152     int bpp = CGImageGetBitsPerPixel(inImage);
    153     int bpl = CGImageGetBytesPerRow(inImage);
    154 
    155     UInt8 * m_PixelBuf = (UInt8 *) CFDataGetBytePtr(m_DataRef);
    156     int length = CFDataGetLength(m_DataRef);
    157 
    158     NSLog(@"len %d", length);
    159     NSLog(@"width=%d, height=%d", width, height);
    160     NSLog(@"1=%d, 2=%d, 3=%d", bpc, bpp,bpl);
    161 
    162     for (int index = 0; index < length; index += 4)
    163     { 
    164     m_PixelBuf[index + 0] = 255 - m_PixelBuf[index + 0];// b
    165     m_PixelBuf[index + 1] = 255 - m_PixelBuf[index + 1];// g
    166     m_PixelBuf[index + 2] = 255 - m_PixelBuf[index + 2];// r
    167     }
    168 
    169     ctx = CGBitmapContextCreate(m_PixelBuf, width, height, bpb, bpl, CGImageGetColorSpace( inImage ), kCGImageAlphaPremultipliedFirst );
    170     CGImageRef imageRef = CGBitmapContextCreateImage (ctx);
    171     UIImage* rawImage = [UIImage imageWithCGImage:imageRef];
    172     CGContextRelease(ctx);
    173     return rawImage;
    174     }
    175 
    176  
    177 
    178 7. 显示图像数据区。
    179 
    180 (显示图像数据区,也就是unsigned char*转为graphics context或者UIImage或和CGImageRef)
    181 
    182     CGContextRef ctx = CGBitmapContextCreate(pixelBuf,width,height, bitsPerComponent,bypesPerLine, colorSpace,kCGImageAlphaPremultipliedLast );
    183     CGImageRef imageRef = CGBitmapContextCreateImage (ctx);
    184     UIImage* image = [UIImage imageWithCGImage:imageRef];
    185     NSString* path = [[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"] stringByAppendingPathComponent:@"ss.png"];
    186     [UIImagePNGRepresentation(self.image) writeToFile:path atomically:YES];
    187     CGContextRelease(ctx);
    188 
    189 得到图像数据区后就可以很方便的实现图像处理的算法
  • 相关阅读:
    面试笔试
    scala(9) Monad
    scala (8) 模糊匹配
    scala (7) Set and Tuple
    scala (6) Map
    scala (5) 可变序列和不可变序列
    scala (4) 可变数组和不可变数组
    scala (3) Function 和 Method
    scala (2) while 和变量
    scala (1) for 循环
  • 原文地址:https://www.cnblogs.com/codemakerhj/p/4991671.html
Copyright © 2011-2022 走看看