Swiftgram/docs/_docs/image-modification-block.md

1.6 KiB
Executable File

title layout permalink prevPage nextPage
Image Modification Blocks docs /docs/image-modification-block.html inversion.html placeholder-fade-duration.html

Many times, operations that would affect the appearance of an image you're displaying are big sources of main thread work. Naturally, you want to move these to a background thread.

By assigning an imageModificationBlock to your imageNode, you can define a set of transformations that need to happen asynchronously to any image that gets set on the imageNode.

SwiftObjective-C
_backgroundImageNode.imageModificationBlock = ^(UIImage *image) {
	UIImage *newImage = [image applyBlurWithRadius:30 
										 tintColor:[UIColor colorWithWhite:0.5 alpha:0.3] 
							 saturationDeltaFactor:1.8 
							 			 maskImage:nil];
	return newImage ? newImage : image;
};

//some time later...

_backgroundImageNode.image = someImage;

backgroundImageNode.imageModificationBlock = { image in
    let newImage = image.applyBlurWithRadius(30, tintColor: UIColor(white: 0.5, alpha: 0.3), 
    								 saturationDeltaFactor: 1.8, 
    								 			 maskImage: nil)
    return (newImage != nil) ? newImage : image
}

//some time later...

backgroundImageNode.image = someImage

The image named "someImage" will now be blurred asynchronously before being assigned to the imageNode to be displayed.