仿麵包旅行個人中心下拉頂部背景放大高斯模糊效果,下拉高斯

來源:互聯網
上載者:User

仿麵包旅行個人中心下拉頂部背景放大高斯模糊效果,下拉高斯

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部落格園看到本文,請點擊查看原文.

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.