简体   繁体   中英

iPhone screenshot of transformed UIImageView

My problem is this -

I have a UIImageView which a user can rotate freely.

I would now like to be able to capture just this newly rotated image.

The code below, which can take a screenshot of an individual view, unfortunately returns a screenshot of the view in its unrotated state.

-(IBAction)captureScreen:(id)sender
{
    UIGraphicsBeginImageContext(imgView.frame.size);
    [imgView.layer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage *screenshot = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    UIImageWriteToSavedPhotosAlbum(screenshot, nil, nil, nil);
}

Any ideas how can I retain the rotation here?

在包含旋转视图的视图上使用代码,而不在旋转视图本身上使用代码。

Try this?

UIGraphicsBeginImageContext(self.view.bounds.size); 
    [self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    UIImageWriteToSavedPhotosAlbum(viewImage, nil, nil, nil);
    NSString *yourPath;
    NSString  *jpgPath = [NSHomeDirectory() stringByAppendingPathComponent:yourPath];
    [UIImageJPEGRepresentation(viewImage, 1.0) writeToFile:jpgPath atomically:YES];

I'm assuming you are rotating the image through the UIImage methods. Problem with that is that it's not really rotating the image itself, it's just telling whoever draws it to draw it sideways.

I would suggest you just actually rotate the image using core graphics and then you can save the result that way

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