2010-05-16 7 views
0

Je montre une image dans un UIImageView (dans un UIScrollView) qui est également stocké dans CoreData.Rotation UIImage

Dans l'interface, je souhaite que l'utilisateur puisse faire pivoter l'image de 90 degrés. Je veux aussi qu'il soit enregistré dans CoreData.

Que dois-je faire pivoter sur l'affichage? le scrollview, le uiimageview ou l'image elle-même? (Si possible, j'aimerais que la rotation soit animée) Mais je dois aussi enregistrer l'image dans CoreData.

J'ai pensé à changer l'orientation de l'image mais cette propriété est en lecture seule.

Répondre

1

Pour afficher simplement l'image pivotée, vous devez faire pivoter l'UIImageView.

Vous pouvez stocker des métadonnées avec l'image dans CoreData indiquant la rotation à appliquer.

Certains formats d'image ont une propriété de rotation implicite. Si vous connaissez le format de données d'image compressé, vous pouvez consulter la spécification et voir si elle prend en charge la spécification.

Si vous souhaitez réellement faire pivoter les pixels de l'image, vous devrez le faire manuellement. Vous pouvez créer un CGBitmapContext et dessiner l'image dans celle-ci pivotée en jouant avec la matrice de transformation, puis créer une nouvelle image à partir du bitmap.

+0

Je stocke effectivement les images au format JPEG et je pense que cela prend en charge l'orientation. Mais puis-je accéder à cela à partir de Cocoa? – Kamchatka

1

Pour l'animation ur tourner ImageView en le transformant:

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

    imageViewObject.transform = CGAffineTransformMakeRotation(angle); 

[UIView commitAnimations]; 

et lorsque vous enregistrez l'image en données de base, puis avant l'image d'économie faites pivoter cette image à l'angle imageViewRotated de la position actuelle. pour une rotation UIImage, utilisez ceci: // rappelez-vous que l'angle doit être en radian, s'il n'est pas en radian, puis convertissez l'angle en radian.

- (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; 

} 

J'espère que cela vous aide.