我有一个模态视图,我想在其中显示计算结果。为了使所有iOS7看起来,我想要一个带有背景的弹出视图的有色背景。
我设法使用文件" UIImage + ImageEffects .h / m"来自苹果。
这是模态视图的viewDidLoad:
- (void)viewDidLoad {
[super viewDidLoad];
/* How it works
self.image is a screenshot of the VC presenting the modal view
1- we set the background as the screenshot of the previous viewControler
2- we hide the popup view
3- we set the background of the popup as a blured snapshot of its container view (self.view)
4- we unhide the popup view and set its radius
5- we create a tinted version of the background image (what we got from the previous view controller
6- we set the background of self.view to the tinted version
*/
// *** 1 ***
self.view.backgroundColor=[UIColor colorWithPatternImage:self.image];
// *** 2 ***
self.containerView.hidden=YES;
// *** 3 ***
UIImage *pepe=[self bluredImageFromView:self.containerView withBackground:self.view withBackgroundTintColor:[UIColor colorWithWhite:1 alpha:0.75]];
self.containerView.backgroundColor=[UIColor colorWithPatternImage:pepe];
// *** 4 ***
self.containerView.hidden=NO;
self.containerView.layer.cornerRadius=4.5f;
// *** 5 ***
UIImage *tintedBackground =[self.image applyBlurWithRadius:0
tintColor:[UIColor colorWithRed:0.85 green:0.85 blue:0.85 alpha:0.35]
saturationDeltaFactor:1.8
maskImage:nil];
// *** 6 ***
self.view.backgroundColor=[UIColor colorWithPatternImage:tintedBackground];
}
它在模拟器上工作得非常好但是当我尝试使用我的iPhone(4S)时,当呈现模态视图时,我有一个黑屏。没有错误,没有从控制台抱怨,没有任何东西。
有什么想法吗?
修改
我添加了代码:
if (!self.image) NSLog (@"self.image is empty");
在viewDidLoad的最开头
似乎在模拟器上,self.image不是零,而在设备上它是。我试图在父VC上从 prepareForSegue 实例化self.image但是没有用。
答案 0 :(得分:2)
我不知道您的bluredImageFromView
是如何工作的,但模拟器与实际的iOS设备(尤其是resizableSnapshotViewFromRect:
和drawViewHierarchyInRect:
)之间存在安全性(沙盒)差异可以解释为什么它有效(但不是在设备上。)
在raywenderlich.com查看此thread,其中有人从Apple Developer技术支持获得建议。这个示例代码显然来自Apple。
- (IBAction)doBlurAndCrop:(id)sender {
UIImage *snapshotImage;
/* draw the image of all the views below our view */
UIGraphicsBeginImageContextWithOptions(self.sourceImageView.bounds.size, NO, 0);
BOOL successfulDrawHierarchy = [self.sourceImageView drawViewHierarchyInRect:self.sourceImageView.bounds afterScreenUpdates:YES];
if ( successfulDrawHierarchy ) {
snapshotImage = UIGraphicsGetImageFromCurrentImageContext();
} else {
NSLog(@"drawViewHierarchyInRect:afterScreenUpdates: failed - there's nothing to draw...");
}
UIGraphicsEndImageContext();
if ( successfulDrawHierarchy ) {
/* calculate the coordinates of the rectangle we're interested in within the returned image */
CGRect cropRect = CGRectOffset(self.targetImageView.frame, - self.sourceImageView.frame.origin.x, - self.sourceImageView.frame.origin.y);
/* draw the cropped section with a clipping region */
UIGraphicsBeginImageContextWithOptions(cropRect.size, YES, 0);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextClipToRect(context, CGRectMake(0, 0, cropRect.size.width, cropRect.size.height));
CGRect targetRectangeForCrop = CGRectMake(-cropRect.origin.x, -cropRect.origin.y, snapshotImage.size.width, snapshotImage.size.height);
[snapshotImage drawInRect:targetRectangeForCrop];
UIImage *croppedSnapshotImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
/* apply a special effect to the resulting image and copy it into place on screen */
UIColor *tintColor = [UIColor colorWithWhite:1.0 alpha:0.3];
self.targetImageView.image = [croppedSnapshotImage applyBlurWithRadius:5 tintColor:tintColor saturationDeltaFactor:1.8 maskImage:nil];
}
}