2013-08-19 30 views
2

我一直試圖做到這一點,因爲永遠。我有一個攝像頭覆蓋。我想讓我的最終圖像成爲從內置攝像頭觀看圖像的一部分。 我所做的是使CGRect的尺寸等於相機中的平方。然後我嘗試使用此功能裁剪它。使用CGRect的裁剪圖像

- (UIImage *)imageByCropping:(UIImage *)imageToCrop toRect:(CGRect)rect 
{ 
    CGImageRef imageRef = CGImageCreateWithImageInRect([imageToCrop CGImage], rect); 

    UIImage *croppedImage = [UIImage imageWithCGImage:imageRef]; 
    CGImageRelease(imageRef); 

    return croppedImage; 
} 

我把它叫做這樣

CGRect rect = CGRectMake(10, 72, 300, 300); 

UIImage *realImage = [self imageByCropping:[self.capturedImages objectAtIndex:0] toRect:rect]; 

我得到的是方向錯誤的不良品質的圖像。

::編輯::

隨着尼廷的答案,我可以裁剪屏幕的正確部分,但問題是它作物下面的攝像機視圖「確認視圖」的視圖。我懷疑這是因爲尼廷的代碼使用

UIImage *screenshot = UIGraphicsGetImageFromCurrentImageContext();

而且因爲視圖控制器中,這一切正在發生的事情,因爲視圖控制器的確認瀏覽是其中正在執行此代碼的控制器。我會盡量用小地圖

CameraOverlay.xib來解釋這一點(它使用此XIB創建一個疊加)< ===== CameraOverlayViewController ---------> ConfirmationView

所以當第一次觸發ViewController時(Tab鍵上的按鈕),它會打開相機(UIImagePickerController)並在其上覆蓋。然後,一旦用戶單擊圖像,圖像將顯示在ConfirmationView上。

我認爲正在發生的事情是,當正在執行 UIGraphicsBeginImageContextWithOptions(self.view.frame.size, YES, 1.0); [self.view.layer renderInContext:UIGraphicsGetCurrentContext()]; UIImage *screenshot = UIGraphicsGetImageFromCurrentImageContext(); 這些線路,當時的看法是ConfirmationView。

注:我稱之爲

(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info方法的功能。

回答

0

請參閱Drawing and printing Guide

CoreGraphics和UIKit之間的默認座標系不同。我認爲你的問題是因爲這個事實。

使用這些可以幫助您解決問題

CGContextRef context = UIGraphicsGetCurrentContext(); 
CGContextTranslateCTM(context , 0.0, rect.size.height); 
CGContextScaleCTM(context , 1.0, -1.0); 
+0

謝謝您的回答。我認爲這個代碼會和我對Nitin的答案有同樣的問題。請你看看編輯? – nupac