#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); }