zoukankan      html  css  js  c++  java
  • 动态将彩色图片动画过渡到黑白图片的BlackAndWhiteView

    动态将彩色图片动画过渡到黑白图片的BlackAndWhiteView

    效果如下:

     BlackAndWhiteView.h 与 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 - 私有方法
    - (UIImage *)grayScale:(UIImage *)inputImage
    {
        int width = inputImage.size.width;
        int height = inputImage.size.height;
        
        CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceGray();
        
        CGContextRef context = CGBitmapContextCreate(nil,
                                                     width,
                                                     height,
                                                     8, // bits per component
                                                     0,
                                                     colorSpace,
                                                     kCGBitmapByteOrderDefault);
        
        CGColorSpaceRelease(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 - 重写setter方法
    @synthesize blackAlpha = _blackAlpha;
    - (void)setBlackAlpha:(float)blackAlpha
    {
        _blackAlpha      = blackAlpha;
        _blackView.alpha = blackAlpha;
    }
    
    - (float)blackAlpha
    {
        return _blackAlpha;
    }
    
    @end

    使用的源码:

    //
    //  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 performSelector:@selector(run) withObject:nil afterDelay:8];
    }
    
    - (void)run
    {
        [UIView animateWithDuration:2 animations:^{
            _blackView.blackAlpha = 1.f;
        }];
    }
    
    @end
  • 相关阅读:
    用python2和python3伪装浏览器爬取网页
    详解python2 和 python3的区别[附实例]
    两种判断(抓取)网页编码的方法【python版】
    python用两种方法实现url短连接
    2013年1月编程语言排行榜榜单: ObjectiveC继续增长
    年初给力!教你自己动手做手机APP应用!!
    [原创]用python求第1000个质数的值
    linux下如何安装配置redis及主从配置
    第四次博客作业结对项目
    2sat的一些总结
  • 原文地址:https://www.cnblogs.com/YouXianMing/p/4006411.html
Copyright © 2011-2022 走看看