I don't know how your bluredImageFromView
is working, but there are security(sandbox) differences between the simulator and an actual iOS device (particularly with resizableSnapshotViewFromRect:
and drawViewHierarchyInRect:
) which might explain why it works (but then doesn't on a device.)
Check out this thread at raywenderlich.com where someone got advice from Apple Developer tech support. This example code is apparently from 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];
}
}