Blur UIImage outside of bounds (Photoshop-style)
I’m trying to do a Gaussian blur on a UIImage that replicates my photoshop mockup.
In Photoshop, when I run a Gaussian blur filter, the image layer gets larger as a result of the blurred edges.
Observed Behavior: Using GPUImage, I can successfully blur my UIImages. However, the new image is cropped at the original bounds, leaving a hard edge all the way around.
UIImageView.layer.masksToBounds = NO; doesn’t help, as the image is cropped not the view.
I’ve also tried placing the UIImage centered on a larger clear image before blurring, and then resizing. This also didn’t help.
Is there a way to achieve this “Photoshop-style” blur?
UPDATE Working Solution thanks to Brad Larson:
UIImage sourceImage = ... GPUImagePicture *imageSource = [[GPUImagePicture alloc] initWithImage:sourceImage]; GPUImageTransformFilter *transformFilter = [GPUImageTransformFilter new]; GPUImageFastBlurFilter *blurFilter = [GPUImageFastBlurFilter new]; //Force processing at scale factor 1.4 and affine scale with scale factor 1 / 1.4 = 0.7 [transformFilter forceProcessingAtSize:CGSizeMake(SOURCE_WIDTH * 1.4, SOURCE_WIDTH * 1.4)]; [transformFilter setAffineTransform:CGAffineTransformMakeScale(0.7, 0.7)]; //Setup desired blur filter [blurFilter setBlurSize:3.0f]; [blurFilter setBlurPasses:20]; //Chain Image->Transform->Blur->Output [imageSource addTarget:transformFilter]; [transformFilter addTarget:blurFilter]; [imageSource processImage]; UIImage *blurredImage = [blurFilter imageFromCurrentlyProcessedOutputWithOrientation:UIImageOrientationUp];
- Could not insert new outlet connection error
- Pull to refresh and Alamofire using Swift 3
- Expandable tableView in iphone
- How to check whether a string contains white spaces
- How to create a “Build” record in iTunesConnect using a list lacking today's build?
- How is a parent view controller notified that is's child view controller has removed itself?
2 Solutions Collect From Internet About “Blur UIImage outside of bounds (Photoshop-style)”
GPUImage will only produce a result that is processed up to the limits of your image. In order to extend past your image, you’ll need to expand the canvas on which it operates.
To do this, you’ll want to feed your image into a GPUImageTransformFilter, then use
-forceProcessingAtSizeRespectingAspectRatio: to enlarge the working area. However, this will also enlarge the image by default, so to counter that, use a scale transform with your GPUImageTransformFilter to reduce the size of your image relative to the larger area. This will keep it with the same pixel dimensions, while placing it within a larger overall image.
Then all you need to do is feed this output into your blur filter and the blur will now extend past the edge of your original image. The size you force the image to be will depend on how far the blur needs to extend past the original image’s edges.
Try resizing the UIImageView’s bounds to adjust to the blur. A view will clip what is outside of its bounds. Note that in your example, the box blurred in photoshop looks to be about 20% larger than the original image.
UIImageView *image; image.layer.bounds = CGRectMake(0, 0, image.layer.bounds.size.width + 5, image.layer.bounds.size.height + 5);
- How to make an expression clickable on iOS?
- iOS objc_msgSend crash, with no report or warning given
- Array of functions in Swift
- fopen$UNIX2003 fails inside external library
- What is the iOS 6 user agent string?
- Use AppDelegate in today extension
- Swift: Unable to detect linear type Barcodes
- How to get keyboard size to resize UITextView and how to use UIKeyboardFrameEndUserInfoKey with Japanese keyboard?
- React Native Responsive Font Size
- Xcode 4 shows no files after loading project
- AFNetworking 2.0 Send Post Request with URL Parameters
- Download and execute iOS code dynamically at runtime
- How to add custom image in navigation bar button item?
- Dealing with Blocks, completion handlers, dispatch_async vs dispatch_sync
- Jquery-ui sortable doesn't work on touch devices based on Android or IOS