IPhone : iOS8 not taking screenshot of define UIImage Area

on Wednesday, April 1, 2015

In iOS 7 and below, this method works fine, the screen allows objects to be placed on a picture, when the user presses next, it should capture the current state of the screen, so that the user can continue adding (different) objects for the next screen.


However, in iOS 8+ it does not capture it properly (items are missing, I believe it is not capturing the screen correctly) here is the 'next screen' method;



[[(AppDelegate *)[[UIApplication sharedApplication] delegate] buttonPlay] setCurrentTime:0.0];
[[(AppDelegate *)[[UIApplication sharedApplication] delegate] buttonPlay] play];

DressSecondViewController * dressHer = nil;

if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
dressHer = [[[DressSecondViewController alloc]initWithNibName:@"DressSecondViewController~ipad" bundle:nil]autorelease];
}
else
{
dressHer = [[[DressSecondViewController alloc]initWithNibName:@"DressSecondViewController~iphone" bundle:nil]autorelease];
}

if([[UIScreen mainScreen] respondsToSelector:@selector(scale)])
UIGraphicsBeginImageContextWithOptions(self.view.frame.size, NO, [UIScreen mainScreen].scale);
else
UIGraphicsBeginImageContext(self.view.frame.size);

UIView * screenshot = [[UIView alloc]initWithFrame:self.view.frame];
screenshot.backgroundColor = [UIColor clearColor];

NSData *tempArchiveView = [NSKeyedArchiver archivedDataWithRootObject:self.body];
UIView *viewOfSelf = [NSKeyedUnarchiver unarchiveObjectWithData:tempArchiveView];
[screenshot addSubview:viewOfSelf];
//[tempArchiveView release];

NSData *tempArchiveView2 = [NSKeyedArchiver archivedDataWithRootObject:self.eyes];
UIView *viewOfSelf2 = [NSKeyedUnarchiver unarchiveObjectWithData:tempArchiveView2];
[screenshot addSubview:viewOfSelf2];
// [tempArchiveView2 release];


NSData *tempArchiveView8 = [NSKeyedArchiver archivedDataWithRootObject:self.pants];
UIView *viewOfSelf8 = [NSKeyedUnarchiver unarchiveObjectWithData:tempArchiveView8];
[screenshot addSubview:viewOfSelf8];
// [tempArchiveView8 release];

NSData *tempArchiveView4 = [NSKeyedArchiver archivedDataWithRootObject:self.shoes];
UIView *viewOfSelf4 = [NSKeyedUnarchiver unarchiveObjectWithData:tempArchiveView4];
[screenshot addSubview:viewOfSelf4];
// [tempArchiveView4 release];

NSData *tempArchiveView5 = [NSKeyedArchiver archivedDataWithRootObject:self.dress];
UIView *viewOfSelf5 = [NSKeyedUnarchiver unarchiveObjectWithData:tempArchiveView5];
[screenshot addSubview:viewOfSelf5];
// [tempArchiveView5 release];

NSData *tempArchiveView6 = [NSKeyedArchiver archivedDataWithRootObject:self.dress];
UIView *viewOfSelf6 = [NSKeyedUnarchiver unarchiveObjectWithData:tempArchiveView6];
[screenshot addSubview:viewOfSelf6];
// [tempArchiveView6 release];

NSData *tempArchiveView7 = [NSKeyedArchiver archivedDataWithRootObject:self.shirt];
UIView *viewOfSelf7 = [NSKeyedUnarchiver unarchiveObjectWithData:tempArchiveView7];
[screenshot addSubview:viewOfSelf7];
// [tempArchiveView7 release];


[screenshot.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *viewImage1 = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
[screenshot release];
dressHer.bodyImage = viewImage1;
dressHer.hairImage = self.hair.image;

[self.navigationController pushViewController:dressHer animated:YES];


Whilst trying new options, I found if I use this below method, the screenshot is captured, but it captures everything (ie its not releasing other stuff that should be released (like if a menu bar is visible, it still remains visible in the next page when before it wouldn't be, as it's only screen-shotting the UIImage not the whole screen before;



[self.view drawViewHierarchyInRect:self.view.bounds afterScreenUpdates:YES];

UIImage *viewImage1 = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
[screenshot release];
dressHer.bodyImage = viewImage1;
dressHer.hairImage = self.hair.image;

[self.navigationController pushViewController:dressHer animated:YES];

1 comments:

Erb said...

did you find a solution?

what I found is that you can add the needed subviews to the screenshot view. Render as normal and the insert the subviews back to self.view. If you save the view index before that then you can insert the subviews in the same place.

int bodyIndex = [[self.view subviews] indexOfObject:body];
[screenshot addSubview:body];

//render:
[screenshot.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *viewImage1 = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

[self.view insertSubview:body atIndex:bodyIndex];

[screenshot release];

dressHer.bodyImage = viewImage1;
dressHer.hairImage = self.hair.image;

[self.navigationController pushViewController:dressHer animated:YES];

Post a Comment