• 仿面包旅行个人中心下拉顶部背景放大高斯模糊效果


    HeaderView.h

    //
    //  HeaderView.h
    //  仿面包旅行个人中心
    //
    //  Created by wb145230@163.com on 15/5/14.
    //  Copyright (c) 2015年 wb145230. All rights reserved.
    //
    
    #import <UIKit/UIKit.h>
    
    @interface HeaderView : UIView
    
    @property(nonatomic, strong) UIScrollView *imageScrollView;
    @property(nonatomic, strong) UIImageView *imageView;                //背景图片
    @property(nonatomic, strong) UIImageView *imageBackgroundView;      //要改变的背景图片
    
    /**
     *  改变顶部view的大小和高斯效果
     *
     *  @param offset scrollview滑动的记录
     */
    -(void)updateHeaderView:(CGPoint) offset;
    
    @end

    HeaderView.m

    //
    //  HeaderView.m
    //  仿面包旅行个人中心
    //
    //  Created by wb145230@163.com on 15/5/14.
    //  Copyright (c) 2015年 wb145230. All rights reserved.
    //
    
    #import "HeaderView.h"
    #import <Accelerate/Accelerate.h>
    
    @implementation HeaderView
    
    - (instancetype)initWithFrame:(CGRect)frame {
        if (self = [super initWithFrame:frame]) {
            self.imageScrollView = [[UIScrollView alloc] initWithFrame:self.bounds];
            [self addSubview:self.imageScrollView];
            
            UIImage *image = [UIImage imageNamed:@"header_bg"];
            //高斯的背景图片
            self.imageBackgroundView = [[UIImageView alloc] initWithFrame:self.imageScrollView.bounds];
            [self setBlurryImage:image];
            self.imageBackgroundView.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleBottomMargin | UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
            self.imageBackgroundView.contentMode = UIViewContentModeScaleAspectFill;
            [self.imageScrollView addSubview:self.imageBackgroundView];
            
            //原图
            self.imageView = [[UIImageView alloc] initWithFrame:self.imageScrollView.bounds];
            self.imageView.image = image;
            self.imageView.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleBottomMargin | UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
            self.imageView.contentMode = UIViewContentModeScaleAspectFill;
            [self.imageScrollView addSubview:self.imageView];
        }
        
        return self;
    }
    
    /**
     *  通过scrollview的滑动改变顶部view的大小和高斯效果
     *
     *  @param offset scrollview下滑的距离
     */
    -(void)updateHeaderView:(CGPoint) offset {
        if (offset.y < 0) {
            CGRect rect = CGRectMake(0, 0, self.frame.size.width, self.frame.size.height);
            CGFloat delta = fabs(MIN(0.0f, offset.y));
            rect.origin.y -= delta;
            rect.size.height += delta;
            self.imageScrollView.frame = rect;
            self.clipsToBounds = NO;
            
            self.imageView.alpha = fabs(offset.y / (2 * CGRectGetHeight(self.bounds) / 3));
        }
    }
    
    
    /**
     *  高斯图片
     *
     *  @param originalImage 需要高斯的图片
     */
    - (void)setBlurryImage:(UIImage *)originalImage {
        
        dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
            UIImage *blurredImage = [self blurryImage:originalImage withBlurLevel:0.9];
            
            dispatch_async(dispatch_get_main_queue(), ^{
                self.imageView.alpha = 0.0;
                self.imageBackgroundView.image = blurredImage;
            });
        });
        
    }
    
    /**
     *  高斯背景
     *
     *  @param image    需要高斯模糊的图片
     *  @param blur     高斯模糊的值
     *
     *  @return
     */
    - (UIImage *)blurryImage:(UIImage *)image withBlurLevel:(CGFloat)blur {
        if ((blur < 0.0f) || (blur > 1.0f)) {
            blur = 0.5f;
        }
        
        int boxSize = (int)(blur * 100);
        boxSize -= (boxSize % 2) + 1;
        
        CGImageRef img = image.CGImage;
        
        vImage_Buffer inBuffer, outBuffer;
        vImage_Error error;
        void *pixelBuffer;
        
        CGDataProviderRef inProvider = CGImageGetDataProvider(img);
        CFDataRef inBitmapData = CGDataProviderCopyData(inProvider);
        
        inBuffer.width = CGImageGetWidth(img);
        inBuffer.height = CGImageGetHeight(img);
        inBuffer.rowBytes = CGImageGetBytesPerRow(img);
        inBuffer.data = (void*)CFDataGetBytePtr(inBitmapData);
        
        pixelBuffer = malloc(CGImageGetBytesPerRow(img) * CGImageGetHeight(img));
        
        outBuffer.data = pixelBuffer;
        outBuffer.width = CGImageGetWidth(img);
        outBuffer.height = CGImageGetHeight(img);
        outBuffer.rowBytes = CGImageGetBytesPerRow(img);
        
        error = vImageBoxConvolve_ARGB8888(&inBuffer, &outBuffer, NULL, 0, 0, boxSize, boxSize, NULL, kvImageEdgeExtend);
        
        
        if (error) {
            NSLog(@"error from convolution %ld", error);
        }
        
        CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
        CGContextRef ctx = CGBitmapContextCreate(outBuffer.data, outBuffer.width, outBuffer.height, 8, outBuffer.rowBytes, colorSpace, CGImageGetBitmapInfo(image.CGImage));
        
        CGImageRef imageRef = CGBitmapContextCreateImage (ctx);
        UIImage *returnImage = [UIImage imageWithCGImage:imageRef];
        
        //clean up
        CGContextRelease(ctx);
        CGColorSpaceRelease(colorSpace);
        
        free(pixelBuffer);
        CFRelease(inBitmapData);
        
        CGColorSpaceRelease(colorSpace);
        CGImageRelease(imageRef);
        
        return returnImage;
    }
    
    
    @end

    ViewController.h

    //
    //  ViewController.h
    //  仿面包旅行个人中心
    //
    //  Created by wb145230@163.com on 15/5/14.
    //  Copyright (c) 2015年 wb145230. All rights reserved.
    //
    
    #import <UIKit/UIKit.h>
    #import "HeaderView.h"
    
    @interface ViewController : UIViewController<UITableViewDataSource, UITableViewDelegate>
    
    @property(nonatomic, strong) UITableView *tableView;
    @property(nonatomic, strong) HeaderView *headerView;
    
    @end

    ViewController.m

    //
    //  ViewController.m
    //  仿面包旅行个人中心
    //
    //  Created by wb145230@163.com on 15/5/14.
    //  Copyright (c) 2015年 wb145230. All rights reserved.
    //
    
    #import "ViewController.h"
    
    @interface ViewController ()
    
    @end
    
    @implementation ViewController
    
    - (void)viewDidLoad {
        [super viewDidLoad];
        [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent animated:YES];
        
        self.view.backgroundColor = [UIColor whiteColor];
        
        self.tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
        self.tableView.dataSource = self;
        self.tableView.delegate = self;
        self.tableView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
        self.tableView.separatorColor = [UIColor clearColor];
        self.tableView.showsVerticalScrollIndicator = NO;
        
        self.headerView = [[HeaderView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 250)];
        self.tableView.tableHeaderView = self.headerView;
        [self.view addSubview:self.tableView];
        
    }
    
    -(void)scrollViewDidScroll:(UIScrollView *)scrollView {
        [self.headerView updateHeaderView:scrollView.contentOffset];
    }
    
    -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
        return 10;
    }
    
    - (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
        if (cell == nil) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
        }
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell;
    }
    
    - (void)didReceiveMemoryWarning {
        [super didReceiveMemoryWarning];
    }
    
    @end

    效果

    如果你不是在wb145230博客园看到本文,请点击查看原文.

  • 相关阅读:
    48. Rotate Image
    47. Permutations II
    46. Permutations
    45. Jump Game II
    44. Wildcard Matching
    43. Multiply Strings
    42. Trapping Rain Water
    Python_匿名函数
    Python_内置函数之map()
    Python_面向对象_单例模式
  • 原文地址:https://www.cnblogs.com/wb145230/p/4570052.html
Copyright © 2020-2023  润新知