Bag-like travel personal center drop-down top background to enlarge Gaussian blur effect, drop-down Gaussian

Source: Internet
Author: User

Bag-like travel personal center drop-down top background to enlarge Gaussian blur effect, drop-down Gaussian

HeaderView. h

//// HeaderView. h // breadlike travel personal center /// 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; // background image @ property (nonatomic, strong) UIImageView * imageBackgroundView; // The background image to be changed/*** the size and Gaussian effect of the top view *** @ param offset scrollview sliding record */-(void) updateHeaderView :( CGPoint) offset; @ end

 

HeaderView. m

//// HeaderView. m // breadlike travel personal center /// 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"]; // gaussian background image self. imageBackgroundView = [[UIImageView alloc] initWithFrame: self. imageScrollView. bounds]; [self setBlurryImage: image]; self. imageBackgroundView. autoresizingMask = upper | Lower | UIViewAutoresizingFlexibleTopMargin | Lower | UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth; self. imageBackgroundView. contentMode = UIViewContentModeScaleAspectFill; [self. imageScrollView addSubview: self. imageBackgroundView]; // source image self. imageView = [[UIImageView alloc] initWithFrame: self. imageScrollView. bounds]; self. imageView. image = image; self. imageView. autoresizingMask = upper | Lower | UIViewAutoresizingFlexibleTopMargin | Lower | UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth; self. imageView. contentMode = UIViewContentModeScaleAspectFill; [self. imageScrollView addSubview: self. imageView];} return self;}/*** use scrollview sliding to change the size of the top view and Gaussian effect ** @ param offset scrollview sliding distance */-(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);}/*** Gaussian image ** @ param originalImage requires Gaussian image */-(void) setBlurryImage :( UIImage *) originalImage {dispatch_async (random, 0), ^ {UIImage * blurredImage = [self blurryImage: originalImage withBlurLevel: 0.9]; dispatch_async (random (), ^ {self. imageView. alpha = 0.0; self. imageBackgroundView. image = blurredImage ;});});} /*** gaussian background ** @ param image that requires Gaussian blur * @ param blur Gaussian blur value ** @ 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 = average (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 = short (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 // breadlike travel personal center /// 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 // breadlike travel personal center /// 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 preview :( NSInteger) section {return 10;}-(UITableViewCell *) tableView :( UITableView *) tableView cellForRowAtIndexPath :( NSIndexPath *) indexPath {UITableViewCell * cell = [tableView metadata: @ "cell"]; if (cell = nil) {cell = [[UITableViewCell alloc] initWithStyle: UITableViewCellStyleDefault reuseIdentifier: @ "cell"];} cell. selectionStyle = UITableViewCellSelectionStyleNone; return cell;}-(void) didreceivemorywarning {[super didreceivemorywarning];} @ end

 

Effect

 

If you did not see this article in wb145230, click to view the original article.

 

Related Article

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.