Monday 1 December 2014

Create UIImage to a Blur UIImage iOS 8 default blur view

- (UIImage *) blurImage:(UIImage *) image
{
 /*
  What we need:
   CIContext
   CIImage
   CIImage
   
  
  We create a CIContext, a CIImage and a CIFilter.
  
  Then we pass the CIImage to the CIFilter.
  
  We grap the filtered Image from the outputImage-property of the CIFilter and render that with the context to a CGImageRef

  Finally, we create a UIImage from the CGImageRef and return it
  
  */
 
 
 //We create the context first to get that out of the way
 
 //TODO: 1. Create a CIContext with options nil (a default context)
 CIContext * context = [CIContext contextWithOptions:nil];
 
 //Now we create a CIImage that can be filtered with a CIFilter
 
 //TODO: 2. Create a CIImage from the UIImage* image (use -initWithImage)
 CIImage * ciImage = [[CIImage alloc] initWithImage:image];
 
 
 //We create our Filter now and set it up
 
 //TODO: 3. Create a blur-filter. It's name is @"CIGaussianBlur"
 CIFilter * blurFilter = [CIFilter filterWithName:@"CIGaussianBlur"];
 
 
 //TODO: 4. Set the CIImage form Step 2 as the inputImage on the blur-filter
 //HINT: use - setValue:(your blur-filter) froKey:(the input-image-key)
 [blurFilter setValue:ciImage forKey:kCIInputImageKey];
 
 
 //Now we render the CIImage with our blur-filter
 
 //TODO: 5. Create a CGImageRef with the context from Step 1.
 
 //HINT: - createCGImage: fromRect:
 //HINT: use the outputImage-property of the blur-filter
 //HINT: the outputImage knows it's size, because it was created from another image (the UIImage)
 //you can get the required rect from the -extent method of your outputImage
 CGImageRef ref = [context createCGImage:blurFilter.outputImage fromRect:[blurFilter.outputImage extent]];
 
 
 
 //Finaly, we create a new UIImage from the CGImageRef
 
 //TODO: 6. create a UIImage from the CGImageRef.
 //HINT: There's a convenience-static-method in the UIImage for that: +imageWithCGImage:
 return [UIImage imageWithCGImage:ref];
}

No comments:

Post a Comment