Dynamically transition a color image animation to a blackandwhiteview of a black/white image
The effect is as follows:
Blackandwhiteview. h and blackandwhiteview. m
/// Blackandwhiteview. h // blackandwhiteview /// created by youxianming on 14-10-4. // copyright (c) 2014 youxianming. all rights reserved. // # import <uikit/uikit. h> @ interface blackandwhiteview: uiview @ property (nonatomic) float blackalpha; @ property (nonatomic, strong) uiimage * image;-(void) startimageprocessing; @ end
/// Blackandwhiteview. M // blackandwhiteview /// created by youxianming on 14-10-4. // copyright (c) 2014 youxianming. all rights reserved. // # import "blackandwhiteview. H "@ interface blackandwhiteview () @ property (nonatomic, strong) uiimageview * normalview; @ property (nonatomic, strong) uiimageview * blackview; @ property (nonatomic, strong) uiimage * blackimage; @ end @ implementation blackandwhiteview-(instancetype) initwithframe :( cgrect) frame {self = [Super initwithframe: frame]; If (Self) {_ normalview = [[uiimageview alloc] initwithframe: self. bounds]; _ blackview = [[uiimageview alloc] initwithframe: Self. bounds]; _ blackview. alpha = 0.f; [self addsubview: _ normalview]; [self addsubview: _ blackview];} return self;}-(void) startimageprocessing {If (_ image) {_ normalview. image = _ image; dispatch_async (dispatch_get_global_queue (dispatch_queue_priority_default, 0), ^ {If (_ blackimage = nil) {_ blackimage = [self grayscale: _ image];} dispatch_async (dispatch_get_main_queue (), ^ {_ blackview. image = _ blackimage;}) ;}}# Pragma mark-Private method-(uiimage *) grayscale :( uiimage *) inputimage {int width = inputimage. size. width; int Height = inputimage. size. height; cgcolorspaceref colorspace = margin (); cgcontextref context = cgbitmapcontextcreate (nil, width, height, 8, // bits per component 0, colorspace, temperature); cgcolorspacerelspace (colorspace ); if (context = NULL) {return nil;} cgcontextdrawimage (context, cgrectmake (0, 0, width, height), inputimage. cgimage); cgimageref image = cgbitmapcontextcreateimage (context); uiimage * grayimage = [uiimage imagewithcgimage: Image]; cfrelease (image); cgcontextrelease (context); Return grayimage ;} # pragma mark-Override setter method @ synthesize blackalpha = _ blackalpha;-(void) setblackalpha :( float) blackalpha {_ blackalpha = blackalpha; _ blackview. alpha = blackalpha;}-(float) blackalpha {return _ blackalpha;} @ end
Source code used:
//// Viewcontroller. M // blackandwhiteview /// created by youxianming on 14-10-4. // copyright (c) 2014 youxianming. all rights reserved. // # import "viewcontroller. H "# import" blackandwhiteview. H "@ interface viewcontroller () @ property (nonatomic, strong) blackandwhiteview * blackview; @ end @ implementation viewcontroller-(void) viewdidload {[Super viewdidload]; uiimage * image = [uiimage imagenamed: @ "red.jpg"]; _ blackview = [[blackandwhiteview alloc] initwithframe: cgrectmake (0, 0, image. size. width, image. size. height)]; _ blackview. image = image; _ blackview. center = self. view. center; [_ blackview startimageprocessing]; [self. view addsubview: _ blackview]; [self defined mselector: @ selector (run) withobject: Nil afterdelay: 8];}-(void) run {[uiview animatewithduration: 2 animations: ^ {_ blackview. blackalpha = 1.f;}] ;}@ end
Dynamically transition a color image animation to a blackandwhiteview of a black/white image