2013-04-23 20 views
-2

我有一個iPad應用程序,我正在使用相機。原始圖像爲480 * 640。我試圖把它調整到124×160,然後使用此代碼,我在互聯網上找到它存儲在CoreData:爲什麼此代碼調整圖像大小將其與原始圖像相比旋轉90度?

- (UIImage *)resizeImage:(UIImage*)image newSize:(CGSize)newSize { 

CGRect newRect = CGRectIntegral(CGRectMake(0, 0, newSize.width, newSize.height)); 
CGImageRef imageRef = image.CGImage; 

UIGraphicsBeginImageContextWithOptions(newSize, NO, 0); 
CGContextRef context = UIGraphicsGetCurrentContext(); 

// Set the quality level to use when rescaling 
CGContextSetInterpolationQuality(context, kCGInterpolationHigh); 
CGAffineTransform flipVertical = CGAffineTransformMake(1, 0, 0, -1, 0, newSize.height); 

CGContextConcatCTM(context, flipVertical); 

// Draw into the context; this scales the image 
CGContextDrawImage(context, newRect, imageRef); 

// Get the resized image from the context and a UIImage 
CGImageRef newImageRef = CGBitmapContextCreateImage(context); 
UIImage *newImage = [UIImage imageWithCGImage:newImageRef]; 

CGImageRelease(newImageRef); 
UIGraphicsEndImageContext(); 

return newImage; 

}

圖像傳回對我來說逆時針旋轉90度,我不明白爲什麼。我曾嘗試評論此聲明:

CGAffineTransform flipVertical = CGAffineTransformMake(1, 0, 0, -1, 0, newSize.height); 

但它沒有區別。這裏有什麼問題?

+1

不是圖像的旋轉元數據的屬性而不是像素的實際重新排列?至少我認爲它適用於使用iPod/iPhone/iPad相機(以及許多其他消費類相機)拍攝的照片 – 2013-04-23 18:23:21

+3

複製粘貼代碼片段中的他死於複製粘貼的代碼片段。想在這裏做的事情是想。 UIImage具有方向信息。 CGImage沒有。因此,您將這些信息丟棄並且從不恢復。 – matt 2013-04-23 18:32:45

+0

好的......我已經嘗試了所有在這裏提出的建議,沒有任何幫助......我瞭解馬特說的是什麼,但這並沒有幫助......我想要做的就是通過縮放圖像節省空間......看起來這是一點回報了大量的工作...... – SpokaneDude 2013-04-23 19:31:05

回答

0

感謝大家誰提出的建議。我一直在尋找,發現這一點,它可以擴展並保持方向:

CGRect screenRect = CGRectMake(0, 0, 120.0, 160.0); 
    UIGraphicsBeginImageContext(screenRect.size); 
    [image drawInRect:screenRect blendMode:kCGBlendModePlusDarker alpha:1]; 
    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext(); 
    UIGraphicsEndImageContext(); 
相關問題