2010-05-16 80 views
0

我在UIImageView(在UIScrollView中)中顯示圖像,該圖像也存儲在CoreData中。UIImage旋轉

在界面中,我希望用戶能夠將圖片旋轉90度。我也希望它被保存在CoreData中。

我應該在顯示屏上旋轉什麼? scrollview,uiimageview或圖像本身? (如果可能,我想旋轉動畫)但是,我還必須將圖片保存到CoreData。

我想過改變圖像的方向,但這個屬性是隻讀的。

回答

1

要只顯示旋轉的圖像,您應該旋轉UIImageView。

您可以在CoreData中存儲一些元數據以及圖片,說明應該應用什麼樣的旋轉。

某些圖像格式具有隱式旋轉屬性。如果你知道壓縮的圖像數據格式,你可以查看規格並查看它是否支持它。

如果您要實際旋轉圖像像素,則必須手動執行此操作。您可以創建一個CGBitmapContext,並通過與變換矩陣混合來將圖像繪製到其中,然後從位圖創建一個新圖像。

+0

我實際上存儲圖像爲JPEG,我認爲它支持方向。但我可以從Cocoa訪問嗎? – Kamchatka 2010-05-17 03:49:38

1

對於動畫通過將其旋轉烏爾ImageView的:

[UIView beginAnimations:nil context:NULL]; 
[UIView setAnimationDuration:0.3]; 
[UIView setAnimationRepeatAutoreverses:NO]; 
[UIView setAnimationRepeatCount:0]; 

    imageViewObject.transform = CGAffineTransformMakeRotation(angle); 

[UIView commitAnimations]; 

,當你保存圖像爲核心的數據,然後保存圖像之前旋轉該圖像從當前位置imageViewRotated角度。 旋轉UIImage使用這個://記住角度應該是弧度,如果它不是弧度然後將角度轉換成弧度。

- (UIImage*) rotateInRadians:(float)radians 
{ 
    const size_t width = self.size.width; 
    const size_t height = self.size.height; 

    CGRect imgRect = (CGRect){.origin.x = 0.0f, .origin.y = 0.0f, .size.width = width, .size.height = height}; 
    CGRect rotatedRect = CGRectApplyAffineTransform(imgRect, CGAffineTransformMakeRotation(radians)); 

    /// Create an ARGB bitmap context 
    CGContextRef bmContext = CreateARGBBitmapContext(rotatedRect.size.width, rotatedRect.size.height, 0); 
    if (!bmContext) 
     return nil; 

    CGContextSetShouldAntialias(bmContext, true); 
    CGContextSetAllowsAntialiasing(bmContext, true); 
    CGContextSetInterpolationQuality(bmContext, kCGInterpolationHigh); 

    /// Rotation happen here (around the center) 
    CGContextTranslateCTM(bmContext, +(rotatedRect.size.width * 0.5f), +(rotatedRect.size.height * 0.5f)); 
    CGContextRotateCTM(bmContext, radians); 

    /// Draw the image in the bitmap context 
    CGContextDrawImage(bmContext, (CGRect){.origin.x = -(width * 0.5f), .origin.y = -(height * 0.5f), .size.width = width, .size.height = height}, self.CGImage); 

    /// Create an image object from the context 
    CGImageRef rotatedImageRef = CGBitmapContextCreateImage(bmContext); 
    UIImage* rotated = [UIImage imageWithCGImage:rotatedImageRef]; 

    /// Cleanup 
    CGImageRelease(rotatedImageRef); 
    CGContextRelease(bmContext); 

    return rotated; 

} 

我希望這可以幫助你。