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