zoukankan      html  css  js  c++  java
  • UIVIEW图片截取和保存图片

    代码用到了Quartz Framework 和 Core Graphics Framework. 在workspace的framework目录里添加这两个framework

    图片截取

    UIGraphicsBeginImageContext(view.bounds.size);
    [view.layer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage *image=UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    图片保存至本地

    static int _index = 0;
    NSString *path = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES) objectAtIndex:0] stringByAppendingFormat:@"/%d.png",_index];
    if ([UIImagePNGRepresentation(image) writeToFile:path atomically:YES]) {
         index += 1;
         NSLog(@"Succeeded!");
    }
    else {
         NSLog(@"Failed!");
    }

    1. 从UIView中获取图像相当于窗口截屏。

    (ios提供全局的全屏截屏函数UIGetScreenView(). 如果需要特定区域的图像,可以crop一下)

    CGImageRef screen = UIGetScreenImage();
    
    UIImage* image = [UIImage imageWithCGImage:screen];

    2. 对于特定UIView的截屏。

    (可以把当前View的layer,输出到一个ImageContext中,然后利用这个ImageContext得到UIImage)

    -(UIImage*)captureView: (UIView *)theView
    
    {
    
    CGRect rect = theView.frame;
    
    UIGraphicsBeginImageContext(rect.size);
    
    CGContextRef context =UIGraphicsGetCurrentContext();
    
    [theView.layer renderInContext:context];
    
    UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
    
    UIGraphicsEndImageContext();
    
    return img;}

     

    3. 如果需要裁剪指定区域。

    (可以path & clip,以下例子是建一个200x200的图像上下文,再截取出左上角)

    UIGraphicsBeginImageContext(CGMakeSize(200,200));
    CGContextRefcontext=UIGraphicsGetCurrentContext();
    UIGraphicsPushContext(context);
    // ...把图写到context中,省略[indent]CGContextBeginPath();
    CGContextAddRect(CGMakeRect(0,0,100,100));//截图区域
    CGContextClosePath();[/indent]CGContextDrawPath();
    CGContextFlush(); // 强制执行上面定义的操作
    UIImage* image = UIGraphicGetImageFromCurrentImageContext();
    UIGraphicsPopContext();

    4. 存储图像。

    (分别存储到home目录文件和图片库文件。)

    存储到目录文件是这样

    1. NSString *path = [[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"] stringByAppendingPathComponent:@"image.png"];
    2. [UIImagePNGRepresentation(image) writeToFile:path atomically:YES];

    若要存储到图片库里面

    1. UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil);

    5.  互相转换UImage和CGImage。

    (UImage封装了CGImage, 互相转换很容易)

    UIImage* imUI=nil;
    CGImageRef imCG=nil;
    imUI = [UIImage initWithCGImage:imCG];
    imCG = imUI.CGImage;

    6. 从CGImage上获取图像数据区。

    (在apple dev上有QA, 不过好像还不支持ios)

    下面给出一个在ios上反色的例子

    -(id)invertContrast:(UIImage*)img
    {
    CGImageRef inImage = img.CGImage; 
    CGContextRef ctx;
    CFDataRef m_DataRef;
    m_DataRef = CGDataProviderCopyData(CGImageGetDataProvider(inImage)); 
    int width = CGImageGetWidth( inImage );
    int height = CGImageGetHeight( inImage );
    int bpc = CGImageGetBitsPerComponent(inImage);
    int bpp = CGImageGetBitsPerPixel(inImage);
    int bpl = CGImageGetBytesPerRow(inImage);
    UInt8 * m_PixelBuf = (UInt8 *) CFDataGetBytePtr(m_DataRef);
    int length = CFDataGetLength(m_DataRef);
    NSLog(@"len %d", length);
    NSLog(@"width=%d, height=%d", width, height);
    NSLog(@"1=%d, 2=%d, 3=%d", bpc, bpp,bpl);
    for (int index = 0; index < length; index += 4)
    { 
    m_PixelBuf[index + 0] = 255 - m_PixelBuf[index + 0];// b
    m_PixelBuf[index + 1] = 255 - m_PixelBuf[index + 1];// g
    m_PixelBuf[index + 2] = 255 - m_PixelBuf[index + 2];// r
    }
    ctx = CGBitmapContextCreate(m_PixelBuf, width, height, bpb, bpl, CGImageGetColorSpace( inImage ), kCGImageAlphaPremultipliedFirst );
    CGImageRef imageRef = CGBitmapContextCreateImage (ctx);
    UIImage* rawImage = [UIImage imageWithCGImage:imageRef];
    CGContextRelease(ctx);
    return rawImage;
    }

     

    7. 显示图像数据区。

    (显示图像数据区,也就是unsigned char*转为graphics context或者UIImage或和CGImageRef)

    CGContextRef ctx = CGBitmapContextCreate(pixelBuf,width,height, bitsPerComponent,bypesPerLine, colorSpace,kCGImageAlphaPremultipliedLast );
    CGImageRef imageRef = CGBitmapContextCreateImage (ctx);
    UIImage* image = [UIImage imageWithCGImage:imageRef];
    NSString* path = [[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"] stringByAppendingPathComponent:@"ss.png"];
    [UIImagePNGRepresentation(self.image) writeToFile:path atomically:YES];
    CGContextRelease(ctx);

    得到图像数据区后就可以很方便的实现图像处理的算法。

  • 相关阅读:
    git常用命令
    国内优秀npm镜像,nvm
    canvas --> getImageData()
    canvas sprite动画 简单封装
    springboot项目中ttf和woff字体图标页面无法显示
    树莓派配置Oracle JDK8
    记一次SqlServer大表查询语句优化和执行计划分析
    linux 查看某个进程和服务内存占用情况命令
    安装MySQL后,需要调整的10个性能配置项
    ARM架构上的Debian10编译timescaledb
  • 原文地址:https://www.cnblogs.com/hushuai-ios/p/3679791.html
Copyright © 2011-2022 走看看