iOS 绘图 剪裁 oc UIGraphics

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
//    self.view.backgroundColor = [UIColor redColor];
    CGRect *rect;
    UIViewSon *son = [[UIViewSon alloc]init];
    [son drawRect:rect];
    
    UIImage  * tree = [UIImage imageNamed:@"hehe"];
    CGSize ts = [tree size];
    //使用uiimage进行绘图
//    UIGraphicsBeginImageContextWithOptions(CGSizeMake(ts.width * 2,ts.height), NO, 0);
//    [tree drawAtPoint:CGPointMake(0, 0)];
//    [tree drawAtPoint:CGPointMake(ts.width, 0)];
//    UIImage *im = UIGraphicsGetImageFromCurrentImageContext();
//    UIGraphicsEndImageContext();
//    
//   
//    UIImageView *imv = [[UIImageView alloc]initWithImage:im];
//    [self.view addSubview:imv];
//    imv.center = self.view.center;
//    
    
    
    
    
    
    
    //使用CGImage进行绘图
    
    /*
    //抽取图片的左右两边
    CGImageRef marLeft = CGImageCreateWithImageInRect([tree CGImage], CGRectMake(0, 0, ts.width / 2, ts.height));
    CGImageRef marRight = CGImageCreateWithImageInRect([tree CGImage], CGRectMake(ts.width / 2, 0, ts.width / 2, ts.height));
    //将图片绘制到画布上去
    
    UIGraphicsBeginImageContextWithOptions(CGSizeMake(ts.width * 1.5, ts.height), NO, 0);
    
    CGContextRef con = UIGraphicsGetCurrentContext();
    
    CGContextDrawImage(con, CGRectMake(0, 0, ts.width / 2, ts.height), flip(marLeft));
    
    CGContextDrawImage(con, CGRectMake(ts.width / 2, 0, ts.width / 2, ts.height), flip(marRight));
    
    UIImage * im = UIGraphicsGetImageFromCurrentImageContext();
    
    UIGraphicsEndImageContext();
    
    //释放内存。arc在这里无效
    CGImageRelease(marRight);
    CGImageRelease(marLeft);
    UIImageView *imv = [[UIImageView alloc]initWithImage:im];
    [self.view addSubview:imv];
    imv.center = self.view.center;
    
    //绘出来的图是上下颠倒的,这不是你的问题,CGContextDrawImage来绘图就会出现这种问题,这主要是由于原始的本地坐标系与目标上下文不匹配
     1.解决方法是如此的奇葩。将CGImage 用CGContextDrawImage绘制到UIImage上,而后再获取对应的CGImage,而后再将CGImage绘制到画布上
    

    */
    
    /*你觉得这样就完了????那你就太天真了,如今又出现了另外一个问题:在双分辨率的设备上,若是咱们的图片文件是高分辨率(@2x)版本,上面的绘图就是错误的。缘由在于对于UIImage来讲,在加载原始图片时使用的imageNamed:方法,它会自动根据所在设备的分辨率类型选择图片,而且UIImage经过设置用来适配的scale属性补偿图片的两倍尺寸。可是一个CGImage对象并无scale属性,它不知道图片文件的尺寸是否为两倍!因此当调用UIImage的CGImage方法,你不能假定所得到的CGImage尺寸与原始UIImage是同样的。在单分辨率和双分辨率下,一个UIImage对象的size属性值都是同样的,可是双分辨率UIImage对应的CGImage是单分辨率UIImage对应的CGImage的两倍大。因此咱们须要修改上面的代码,让其在单双分辨率下均可以工做。代码以下*/
    
    UIImage *mar = [UIImage imageNamed:@"hehe"];
    
    CGSize ms = [mar size];
    
    CGImageRef marCG = [mar CGImage];
    CGSize msCG = CGSizeMake(CGImageGetWidth(marCG), CGImageGetHeight(marCG));
    UIGraphicsBeginImageContextWithOptions(CGSizeMake(ms.width, ms.height), NO, 0);
    CGImageRef imgLeft = CGImageCreateWithImageInRect(marCG, CGRectMake(0, 0, msCG.width / 2, msCG.height));
    CGImageRef imgRight = CGImageCreateWithImageInRect(marCG, CGRectMake(msCG.width / 2, 0, msCG.width / 2, msCG.height));
    
    [[UIImage imageWithCGImage:imgLeft scale:[mar scale] orientation:UIImageOrientationUp] drawAtPoint:CGPointMake(0, 0)];
    [[UIImage imageWithCGImage:imgRight scale:[mar scale] orientation:UIImageOrientationUp] drawAtPoint:CGPointMake(msCG.width / 2, 0)];

    UIImage * img = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    
    //释放内存。arc在这里无效
    CGImageRelease(imgLeft);
    CGImageRelease(imgRight);
    UIImageView *imv = [[UIImageView alloc]initWithImage:img];
    [self.view addSubview:imv];
    imv.center = self.view.center;
    
}


//写一个方法获取图片画布,并将图片先反转绘制在画布上
CGImageRef flip(CGImageRef im)
{
    CGSize sz = CGSizeMake(CGImageGetWidth(im), CGImageGetHeight(im));
    UIGraphicsBeginImageContextWithOptions(sz, NO, 0);
    CGContextDrawImage(UIGraphicsGetCurrentContext(), CGRectMake(0, 0, sz.width, sz.height), im);
    CGImageRef result = [UIGraphicsGetImageFromCurrentImageContext() CGImage];
    return result;
}



/*     CIFilter与CIImage。   */




- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end

这只是图片剪裁的一部分html

 

 

更多UIView绘图参考http://www.cocoachina.com/industry/20140115/7703.htmlui

相关文章
相关标签/搜索