繁体   English   中英

如何在iPhone相册中保存旋转图像?

[英]How to save the rotate image in iphone album?

当我单击视图并且图像旋转成功时,我能够旋转图像,但是旋转的图像未保存在iPhone相册中。

当我单击“保存”按钮时,先前的图像正在保存在相册中,请任何人帮助

- (void) touchesBegan:(NSSet *)_touches withEvent:(UIEvent *)_event
  {
   UITouch* touch = [_touches anyObject];
   CGPoint location = [touch locationInView:m_block];
   m_locationBegan = location;
   }

  - (void) touchesMoved:(NSSet *)_touches withEvent:(UIEvent *)_event
   {
    UITouch* touch = [_touches anyObject];
    CGPoint location = [touch locationInView:m_block];
    [self updateRotation:location];  
   }

- (void) touchesEnded:(NSSet *)_touches withEvent:(UIEvent *)_event
    {
   UITouch *touch=[_touches anyObject];
   CGPoint location = [touch locationInView:m_block];
   m_currentAngle = [self updateRotation:location];    
 }
 - (float) updateRotation:(CGPoint)_location
   {
     float fromAngle = atan2(m_locationBegan.y-m_block.center.y, m_locationBegan.x-m_block.center.x);
    float toAngle = atan2(_location.y-m_block.center.y, _location.x-m_block.center.x);
     float newAngle = wrapd(m_currentAngle + (toAngle - fromAngle), 0, 2*3.14);

     CGAffineTransform cgaRotate = CGAffineTransformMakeRotation(newAngle);
     m_block.transform = cgaRotate;
     int oneInFifty = (newAngle*50)/(2*3.14);
     m_label.text = [NSString stringWithFormat:@"Angle: %f 1in50: %i", newAngle, oneInFifty];
      return newAngle;
    }
  - (IBAction)saveImageToAlbum
     {
   [self.library saveImage:m_block.image toAlbum:@"My" withCompletionBlock:^(NSError *error) {
      if (error!=nil) {
        NSLog(@"Big error: %@", [error description]);
    }
  }];

UIImage *image = m_block.image;
NSUserDefaults * user= [NSUserDefaults standardUserDefaults] ;
[user setObject:UIImagePNGRepresentation(image) forKey:@"foo"];
[user synchronize];
[self dismissViewControllerAnimated:YES completion:nil];

   }

我认为您正在使用自定义UIView或UIImageView。

您可以通过以下代码保存任何视图的当前外观:

+ (UIImage *) imageWithView:(UIView *)view
{
    UIGraphicsBeginImageContextWithOptions(view.bounds.size, view.opaque, 0.0);
    [view.layer renderInContext:UIGraphicsGetCurrentContext()];

    UIImage * img = UIGraphicsGetImageFromCurrentImageContext();

    UIGraphicsEndImageContext();

    return img;
}

我是从这里得到的: 如何在不损失视网膜显示质量的情况下将UIView捕获到UIImage

这对我来说很好。

祝一切顺利...

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM