如何将旋转照片功能添加到MWPhotoBrowser?

时间:2013-12-10 20:18:35

标签: ios objective-c mwphotobrowser

以下是我的尝试。我在导航栏中添加了一个rightBarButtonItem。然后用下面的方法连接它。我究竟做错了什么? 当我单击按钮时,会调用该方法,因为我看到了日志。调用时没有任何可见的事情发生。如果我反复点击按钮,图像会闪烁。

- (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 个答案:

答案 0 :(得分:2)

-(MWPhoto *) photoWithImage方法是否可能无法读取UIImage的orientation属性?我会尝试通过将图像绘制到旋转的图像上下文中来旋转图像,然后从上下文中提取旋转的图像。看看这个帖子: Creating a UIImage from a rotated UIImageView

答案 1 :(得分:0)

我将照片旋转功能添加到MwphotoBrawse.Its成功旋转。可以帮助任何。我的代码是。

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