2012-07-23 54 views
2

我正在爲iOS的縮放和裁剪圖像(類似於camara應用程序)的功能工作,下面的代碼工作正常,只是由此產生的圖像倒過來,我想理解爲什麼。規模和作物圖像ios

感謝

- (UIImage*)imageByCropping:(UIImageView *)imageViewToCrop toRect:(CGRect)rect 
{ 
    //create a context to do our clipping in 
    CGRect newRect = CGRectApplyAffineTransform(rect, imageViewToCrop.transform); 
    UIImage *imageToCrop = imageViewToCrop.image; 
    UIGraphicsBeginImageContext(newRect.size); 
    CGContextRef currentContext = UIGraphicsGetCurrentContext(); 

    //create a rect with the size we want to crop the image to 
    //the X and Y here are zero so we start at the beginning of our 
    //newly created context 
    CGRect clippedRect = CGRectMake(0, 0, rect.size.width, rect.size.height); 
    CGContextClipToRect(currentContext, clippedRect); 


    //draw the image to our clipped context using our offset rect 
    CGContextDrawImage(currentContext, newRect, imageToCrop.CGImage); 

    //pull the image from our cropped context 
    UIImage *cropped = UIGraphicsGetImageFromCurrentImageContext(); 

    //pop the context to get back to the default 
    UIGraphicsEndImageContext(); 

    return cropped; 

} 

回答

0

我也遇到了這個問題,沒有任何解釋。但是找到了解決這個問題的解決方法。只需在你的代碼中放入以下兩行並再次嘗試。這個對我有用。

CGContextTranslateCTM(context, 0.0, newRect.size.height); 
CGContextScaleCTM(context, 1.0, -1.0);//flip context