2014-04-06 102 views
0

我有一個模態視圖,我想要顯示計算結果。爲了讓所有的iOS7看起來,我想要一個帶有模糊背景的彈出視圖的有色背景。UIImage + ImageEffects無法在設備上工作

我設法使用蘋果公司的「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]; 


} 

它工作得很好的模擬器但是當我試圖onmy iPhone(4S),提出了模態視圖的時候,我有一個黑色的屏幕。沒有錯誤,沒有抱怨從控制檯,沒有任何。

有什麼想法?

編輯

我添加代碼:

if (!self.image) NSLog (@"self.image is empty"); 

在viewDidLoad中

之初

看來,在模擬器上self.image不爲零,而在設備上它是。我嘗試從父VC上的prepareForSegue實例化self.image,但不起作用。

回答

2

我不知道你的bluredImageFromView工作,但也有安全性(沙盒)仿真和實際iOS設備(特別是resizableSnapshotViewFromRect:drawViewHierarchyInRect:)之間的差異這或許可以解釋爲什麼它的工作原理(但是不能夠)
看看這個thread在raywenderlich.com有人從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]; 
} 
} 
+0

非常感謝!我實際上是修改了Ray Wenderlicht的教程中的一段剪輯代碼,這正是你指出的那個線程。我使用您提供的這些代碼調整了我的方法,現在它像魅力一樣工作。我不知道該怎麼感謝你才足夠! – Marcal

+0

不客氣。我很想知道'applyBlurWithRadius:tintColor:saturationDeltaFactor:maskImage:'是否適用於iPhone 4. –

+0

我只能告訴你,用我的4s可以完美無瑕。我會在提交應用程序之前幾天在我的iPad 3上嘗試它。 – Marcal

相關問題