简体   繁体   中英

How to save the rotate image in iphone album?

I am able to rotate the image when I click on view and image is rotating successfully but rotated image is not saving in iphone album.

When I click on save button previous image is saving in album please any one help me

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

   }

I think you are using custom UIView Or UIImageView.

You can save current appearance of any View by following code:

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

    UIImage * img = UIGraphicsGetImageFromCurrentImageContext();

    UIGraphicsEndImageContext();

    return img;
}

I get this from : How to capture UIView to UIImage without loss of quality on retina display

This is working very well for me.

All the best...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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