Why does my programmatically created screenshot look so bad on iOS 7?

后端 未结 5 884
梦毁少年i
梦毁少年i 2020-11-29 19:46

I am trying to implement sharing app with facebook. I used this code to take the screenshot:

CGSize imageSize = CGSizeMake(self.view.bounds.size.width, self.         


        
相关标签:
5条回答
  • 2020-11-29 19:55

    in iOS 8 : this is how i am doing to get ScreenShot

    1. just added one UIImageView and Method to take screenshot in .h file

    @property (weak, nonatomic) IBOutlet UIImageView *imageView;

     -(IBAction)takeSnapShot:(id)sender;
    

    2 added code snip for taking screen shot and set on UIImageView in .m file

    - (IBAction)takeSnapShot:(id)sender
    {
        UIGraphicsBeginImageContext(self.view.bounds.size);
        [self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
        UIImage *snapShotImage = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
    
        imageView.image = snapShotImage;
    }
    
    1. below is the output i got.

    enter image description here

    0 讨论(0)
  • 2020-11-29 19:57

    On iOS7 you can have glitches if you use

    [view drawViewHierarchyInRect:view.bounds afterScreenUpdates:YES]
    

    during ongoing animation. Set afterScreenUpdates = NO to get rid of glitches.

    0 讨论(0)
  • 2020-11-29 20:00

    you can try this

    - (UIImage *) screenshot {
        UIGraphicsBeginImageContextWithOptions(self.view.bounds.size, NO, [UIScreen mainScreen].scale);
    
        [self.view drawViewHierarchyInRect:self.view.bounds afterScreenUpdates:YES];
    
        UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
        return image;
    }
    
    0 讨论(0)
  • 2020-11-29 20:08

    New API has been added since iOS 7, that should provide efficient way of getting snapshot

    • snapshotViewAfterScreenUpdates: renders the view into a UIView with unmodifiable content

    • resizableSnapshotViewFromRect:afterScreenUpdates:withCapInsets : same thing, but with resizable insets

    • drawViewHierarchyInRect:afterScreenUpdates: : same thing if you need all subviews to be drawn too (like labels, buttons...)

    You can use the UIView returned for any UI effect, or render in into an image like you did if you need to export.

    I don't know how good this new method performs VS the one you provided (although I remember Apple engineers saying this new API was more efficient)

    0 讨论(0)
  • 2020-11-29 20:15

    Make sure that opaque is set to NO

    0 讨论(0)
提交回复
热议问题