how to add rotate photo functionality to MWPhotoBrowser?

499 views Asked by At

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];  
}
2

There are 2 answers

0
David Schwartz On BEST ANSWER

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

1
amisha.beladiya On

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