0

I am taking a snapshot of an area of my view and setting it to a new UIView.

UIView *currView = [self.view resizableSnapshotViewFromRect:mp.moviePlayer.view.frame afterScreenUpdates:YES withCapInsets:UIEdgeInsetsZero];
    [currView setFrame:CGRectMake(0, 0, 204, 128)];

Then I am trying to set a UIIamgeView image using this:

[image2 setImage:[self getUIImageFromThisUIView:currView]];

-(UIImage*)getUIImageFromThisUIView:(UIView*)aUIView
{
    UIGraphicsBeginImageContext(aUIView.bounds.size);
    [aUIView.layer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return viewImage;
}

But it does not set the UIImageView's image to anything? Thanks in advance

  • That's the right technique for standard UIKit controls (though generally one would use `UIGraphicsBeginImageContextWithOptions` with a scale of 0 to capture retina resolution images), but with `MPMoviePlayerController`, you should use `thumbnailImageAtTime`. See http://stackoverflow.com/questions/13855340/is-there-any-way-to-take-a-screenshot-when-mpmovieplayercontroller-is-playing – Rob Mar 19 '14 at 02:33

1 Answers1

0

I used Category for create image of view,

UIView+Snapshot.h
@interface UIView (Snapshot)

- (UIImage *) getSnapshot;
@end
UIView+Snapshot.m
#import "UIView+Snapshot.h"

@implementation UIView (Snapshot)

- (UIImage *) getSnapshot
{
    UIGraphicsBeginImageContextWithOptions(self.bounds.size, NO, self.window.screen.scale);

    [self drawViewHierarchyInRect:self.frame afterScreenUpdates:YES];
    UIImage *snapshotImage = UIGraphicsGetImageFromCurrentImageContext();

    UIGraphicsEndImageContext();

    return snapshotImage;
}


@end

And add header, for example:

#import "UIView+Snapshot.h"

UIImage *image = [self.view getSnapshot];
Khalil
  • 238
  • 2
  • 15
  • 1
    This is a nice little category (though, using the iOS 7 `drawViewHierarchyInRect` method, so it won't work with earlier iOS versions), but I'm not sure this will solve the problem unique to `MPMoviePlayerController` that defeats typical attempts to take snapshots that work with standard `UIKit` controls. – Rob Mar 19 '14 at 06:03