2014-01-28 63 views
1

我正在嘗試製作一個方法將我的UIImage裁剪到中間。我需要一種方法來裁剪一張照片的左邊中間,另一張裁剪我的另一張照片。在兩半中裁剪UIImage

到目前爲止,我只景觀的左半部分和肖像權:

if(firstImage.imageOrientation == UIImageOrientationUp){ //LANDSCAPE 
     UIImage *image = firstImage; 
     CGImageRef tmpImgRef = image.CGImage; 
     CGImageRef topImgRef = CGImageCreateWithImageInRect(tmpImgRef, CGRectMake(0, 0, image.size.width /2.0, image.size.height)); 
     firstImage = [UIImage imageWithCGImage:topImgRef]; 
     CGImageRelease(topImgRef); 
    } 
    else{  //PORTRAIT 
     UIImage *image = firstImage; 
     CGImageRef tmpImgRef = image.CGImage; 
     CGImageRef topImgRef = CGImageCreateWithImageInRect(tmpImgRef, CGRectMake(0, 0, image.size.width, image.size.height/2.0)); 
     firstImage = [UIImage imageWithCGImage:topImgRef scale:image.scale orientation:UIImageOrientationRight]; 
     CGImageRelease(topImgRef); 
    } 

我不那麼熱衷於核心圖形,能有人給我一個忙嗎? 我現在想要的是有一種方法來告訴CGImageCreateWithImageInRect耕作我在風景右半部分而不是左半部分,並在肖像我需要有左半部。

回答

0

我沒有測試過,但我認爲它可以工作:

//Portrait bottom half 
UIGraphicsBeginImageContextWithOptions(CGSizeMake(image.width,image.height/2.0),NO,0); 
[image drawAtPoint:CGPointMake(0, -image.size.height/2.0) blendMode:kCGBlendModeCopy alpha:1.0]; 
UIImage *croppedImage = UIGraphicsGetImageFromCurrentImageContext(); 
UIGraphicsEndImageContext(); 

//Landscape right half 
UIGraphicsBeginImageContextWithOptions(CGSizeMake(image.width/2.0,image.height),NO,0); 
[image drawAtPoint:CGPointMake(-image.size.width/2.0,0) blendMode:kCGBlendModeCopy alpha:1.0]; 
UIImage *croppedImage = UIGraphicsGetImageFromCurrentImageContext(); 
UIGraphicsEndImageContext();