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);
    
    }
  • 相关阅读:
    PHP js使用ajax异步处理方式请求PHP,解决数组中文乱码
    PHP Apache服务配置
    opencv高斯背景建模
    Opencv,腐蚀,膨胀,轮廓检测,轮廓外接多边形
    opencv删除二值图中较小的噪点色块
    opencv图像操作
    opencv统计二值图黑白像素个数
    JAVA常用工具类
    Netty4 学习笔记之四: Netty HTTP服务的实现
    分享一些JAVA相关资源
  • 原文地址:https://www.cnblogs.com/mins/p/4566377.html
Copyright © 2011-2022 走看看