zoukankan      html  css  js  c++  java
  • 截取图片

    #pragma mark - 截取图片
    
    - (void)cutMapView:(UIView *)theView
    
    {
    
        //************** 得到图片 *******************
    
        CGRect rect = theView.frame;  //截取图片大小
    
        
    
        //开始取图,参数:截图图片大小
    
        UIGraphicsBeginImageContext(rect.size);
    
        //截图层放入上下文中
    
        [theView.layer renderInContext:UIGraphicsGetCurrentContext()];
    
        //从上下文中获得图片
    
        UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    
        //结束截图
    
        UIGraphicsEndImageContext();
    
        
    
        
    
        //************** 存图片 *******************
    
        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);
    
        NSString *filePath = [[paths objectAtIndex:0] stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.png",@"jietu"]];   // 保存文件的名称
    
        NSLog(@"filePath = %@",filePath);
    
        //UIImagePNGRepresentation方法将image对象转为NSData对象
    
        //写入文件中
    
        BOOL result = [UIImagePNGRepresentation(image)writeToFile: filePath atomically:YES]; 
    
        NSLog(@"result = %d",result);
    
        
    
        
    
        //*************** 截取小图 ******************
    
        CGRect rect1 = CGRectMake(90, 0, 82, 82);//创建矩形框
    
        //对图片进行截取
    
        UIImage * image2 = [UIImage imageWithCGImage:CGImageCreateWithImageInRect([image CGImage], rect1)]; 
    
        NSString *filePath2 = [[paths objectAtIndex:0] stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.png",@"jietu2"]];   // 保存文件的名称
    
        NSLog(@"filePath = %@",filePath);
    
        BOOL result2 = [UIImagePNGRepresentation(image2)writeToFile:filePath2 atomically:YES];
    
        NSLog(@"result2 = %d",result2);
    
        
    
        //存入相册
    
        //UIImageWriteToSavedPhotosAlbum(viewImage, nil, nil, nil);
    
    }
  • 相关阅读:
    2.18周四笔记
    Tkinter 控件详细介绍
    python基础: day4作业计算器
    Python的functools.reduce用法
    正则表达式技术深入
    递归
    生成器generator
    绕过验证码
    导入JSONPathExtractorExample.jmx文件报错,导不进去
    jmeter一些插件下载网址
  • 原文地址:https://www.cnblogs.com/mins/p/4566377.html
Copyright © 2011-2022 走看看