2013-02-03 16 views
0

可能重複:
Cropping a UIImage種植UIImage的任何圖像分辨率

我看到很多教程有關裁剪圖像並正嘗試我的運氣,但沒有成功。

我需要從AVFoundation中裁剪圖像。由於從相機拍攝圖像時,它將從肖像模式向左旋轉,因此我還需要將其向右旋轉,並且我的x和y相反。問題是,如果我發送圖像的幀,我認爲它在圖像和矩形的大小沒有相關性。

的代碼是:

@property (weak, nonatomic) IBOutlet UIView *videoPreviewView; 
... 
... 
int width = videoPreviewView.frame.size.width; 
int height = videoPreviewView.frame.size.height; 
int x = videoPreviewView.frame.origin.x; 
int y = videoPreviewView.frame.origin.y; 
CGRect croprect = CGRectMake(y, x,height,width); 
// Draw new image in current graphics context 
CGImageRef imageRef = CGImageCreateWithImageInRect([sourceImage CGImage], croprect); 
// Create new cropped UIImage 
UIImage *resultImage = [UIImage imageWithCGImage:imageRef scale:[sourceImage scale] orientation:UIImageOrientationRight]; 

當我打印的幀的大小我得到:

(4.5,69.5,310,310)

和圖像尺寸爲:

(720,1280)

如何在任何圖像分辨率下進行裁剪?

我試過用值乘以image.scale - 然而,此值爲1.00

+0

請參閱http://stackoverflow.com/questions/158914/cropping-a-uiimage – Spynet

+0

@Spynet我試過了代碼。它不裁剪我的圖像,這是高分辨率 – Dejell

+0

圖像的大小是多少 – Spynet

回答

1

試試這個one.This一定會幫助你的。 https://github.com/barrettj/BJImageCropper

+0

您是否可以使用上述庫的優勢來編輯您的文章? – Dejell

+0

@Odelya樣本項目已經與我發送的鏈接中的圖書館一起提供。 – kumareshm174

+0

我看到了方法calcFrameWithImage - 在我看來,有些東西可以幫助但不知道具體如何。你提到它嗎?也許你想發佈相關的代碼? – Dejell

0
To resize image, Try this  

    UIImage *image = [UIImage imageNamed:@"image.png"]; 

    CGSize itemSize = CGSizeMake((your Width), (your Height)); 
    UIGraphicsBeginImageContext(itemSize); 
    CGRect imageRect = CGRectMake(0.0, 0.0, itemSize.width, itemSize.height); 
    [image drawInRect:imageRect]; 

    UIImage * yourCroppedImage = UIGraphicsGetImageFromCurrentImageContext(); 
    UIGraphicsEndImageContext(); 

To Crop, 

    // Create new image context 
    UIGraphicsBeginImageContext(SIZE); 

    // Create CGRect for image 
    CGRect newRect = CGRectMake(x, y,SIZE.width,SIZE.height); 

    // Draw the image into the rect 
    [ORIGINALIMAGE drawInRect:newRect]; 

    // Saving the image, ending image context 
    UIImage * newImage = UIGraphicsGetImageFromCurrentImageContext(); 

    UIGraphicsEndImageContext(); 
+0

這調整大小,但不裁剪!如果我嘗試設置CGRectMake(x,y,itemSize.width,itemSize.height),我會得到一個空白區域 – Dejell