欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

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

程序员文章站 2022-05-21 23:48:02
...

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

动态将彩色图片动画过渡到黑白图片的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