簡體   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