Dynamically transition a color image animation to a blackandwhiteview of a black/white image

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.