码迷,mamicode.com
首页 > 其他好文 > 详细

图片使用

时间:2015-11-14 15:00:10      阅读:164      评论:0      收藏:0      [点我收藏+]

标签:

- (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);

}

图片使用

标签:

原文地址:http://www.cnblogs.com/zhaoxiaolei2015/p/4964355.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!