1

Below is my attempt. I added a rightBarButtonItem to the navigation bar. Then connected it with the method below. What am I doing wrong? When I click the button, the method is called, because I see the log. Nothing visible happens when it is called. If I click the button repeatedly, the image flickers however.

- (void)rotatedImage
{
    NSLog(@"rotatedImage");
    id <MWPhoto> assetPhoto =  [self photoAtIndex:_currentPageIndex];
    UIImageOrientation imageOrientation = assetPhoto.underlyingImage.imageOrientation;
    UIImageOrientation orientationToBe;

    if (imageOrientation == UIImageOrientationLeft) orientationToBe = UIImageOrientationDown;
    else if (imageOrientation == UIImageOrientationDown) orientationToBe = UIImageOrientationRight;
    else if (imageOrientation == UIImageOrientationRight) orientationToBe = UIImageOrientationUp;
    else orientationToBe = UIImageOrientationLeft;

    UIImage *rotatedImage = [UIImage imageWithCGImage:[assetPhoto.underlyingImage CGImage] scale: 1 orientation:orientationToBe];

    MWPhoto *r = [MWPhoto photoWithImage:rotatedImage];
    [_photos replaceObjectAtIndex:_currentPageIndex withObject:r];
    [self reloadData];  
}
user299648
  • 2,769
  • 6
  • 34
  • 43
  • Is this code called at all? If so, what happens? Provide more details. – rmaddy Dec 10 '13 at 21:14
  • The code is called, because I see the log. Nothing visible happens when it is called. If I click the button repeatedly, the image flickers however. – user299648 Dec 10 '13 at 21:29
  • I also try to rotate image using context.but its not rotate.have you rotate image succesfuly?If yes then how ? – amisha.beladiya Apr 30 '16 at 09:02

2 Answers2

2

is it possible that the -(MWPhoto *) photoWithImage method doesn't read the orientation property of the UIImage? i would try rotating the image by drawing it into a rotated image context and then extracting the rotated image from the context. check out this thread: Creating a UIImage from a rotated UIImageView

Community
  • 1
  • 1
David Schwartz
  • 507
  • 3
  • 14
0

I add the photo rotate functionality to MwphotoBrawse.Its rotate succesfully.May be help to any.My code is.

  id <MWPhoto> assetPhoto =  [self photoAtIndex:_currentPageIndex];
      UIImage *rotatedImage = [UIImage imageWithCGImage:[assetPhoto.underlyingImage CGImage]];
      initWithImage:rotatedImage];

        CGRect screenRect = [[UIScreen mainScreen] bounds];
        CGFloat screenWidth = screenRect.size.width;
        CGFloat screenHeight = screenRect.size.height;
        CGFloat radians = DegreesToRadians(90);

        UIView *rotatedViewBox = [[UIView alloc] initWithFrame:CGRectMake(0,0, screenWidth, screenHeight)];
        CGAffineTransform t = CGAffineTransformMakeRotation(radians);
        rotatedViewBox.transform = t;
        CGSize rotatedSize = rotatedViewBox.frame.size;

        UIGraphicsBeginImageContextWithOptions(rotatedSize, NO, [[UIScreen mainScreen] scale]);
        CGContextRef bitmap = UIGraphicsGetCurrentContext();

        CGContextTranslateCTM(bitmap, rotatedSize.width / 2, rotatedSize.height / 2);

        CGContextRotateCTM(bitmap, radians);

        CGContextScaleCTM(bitmap, 1.0, -1.0);
        CGContextDrawImage(bitmap, CGRectMake(-self.view.bounds.size.width / 2, -self.view.bounds.size.height / 2 , self.view.bounds.size.width, self.view.bounds.size.height),rotatedImage.CGImage );

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


        MWPhoto *r = [MWPhoto photoWithImage:newImage];
        [_photos replaceObjectAtIndex:_currentPageIndex withObject:r];

        [self performLayout];
amisha.beladiya
  • 363
  • 1
  • 12