How to remove red eye from image in iPhone? - iphone

I want to remove red eye effect form photo but not get any sample can any one help me with working demo code or code snippet?
Thanks.

Use below category of UIImage :
#interface UIImage (Utitlities)
-(UIImage*)redEyeCorrection;
#end
#implementation UIImage (Utitlities)
-(UIImage*)redEyeCorrection
{
CIImage *ciImage = [[CIImage alloc] initWithCGImage:self.CGImage];
// Get the filters and apply them to the image
NSArray* filters = [ciImage autoAdjustmentFiltersWithOptions:[NSDictionary dictionaryWithObject:[NSNumber numberWithBool:NO] forKey:kCIImageAutoAdjustEnhance]];
for (CIFilter* filter in filters)
{
[filter setValue:ciImage forKey:kCIInputImageKey];
ciImage = filter.outputImage;
}
// Create the corrected image
CIContext* ctx = [CIContext contextWithOptions:nil];
CGImageRef cgImage = [ctx createCGImage:ciImage fromRect:[ciImage extent]];
UIImage* final = [UIImage imageWithCGImage:cgImage];
CGImageRelease(cgImage);
return final;
}
#end
Usage: Sample code given below
UIImage *redEyeImage = [UIImage imageNamed:#"redEye.jpg"];
if (redEyeImage) {
UIImage *newRemovedRedEyeImage = [redEyeImage redEyeCorrection];
if (newRemovedRedEyeImage) {
imgView.image = newRemovedRedEyeImage;
}
}
Refer NYXImagesKit UIImage Enhancing link

Related

Adding Filters to imageview

I am new to ios development and trying to use code filters...
imgAnimation=[[UIImageView alloc]initWithFrame:frame];
imgAnimation.animationImages=_arrimg;
//animationImages=animationImages;
imgAnimation.contentMode=UIViewContentModeScaleAspectFit;
imgAnimation.animationDuration = 2.0f;
imgAnimation.animationRepeatCount = 0;
[imgAnimation startAnimating];
[self.view addSubview:imgAnimation];
My animation is working properly but how can I apply filters like sepia, grey scale I had found many tutorials but they are for single images kindly help ???
Sample Code :
+(UIImage *) applyFilterToImage: (UIImage *)inputImage
{
CIImage *beginImage = [CIImage imageWithCGImage:[inputImage CGImage]];
CIContext *context = [CIContext contextWithOptions:nil];
CIFilter *filter = [CIFilter filterWithName:#"CISepiaTone" keysAndValues: kCIInputImageKey, beginImage, #"inputIntensity", [NSNumber numberWithFloat:0.8], nil];
CIImage *outputImage = [filter outputImage];
CGImageRef cgimg = [context createCGImage:outputImage fromRect:[outputImage extent]];
UIImage *newImg = [UIImage imageWithCGImage:cgimg];
CGImageRelease(cgimg);
return newImg;
}
Basically you have to pre load all the images with the filters. Then load them to an array and set it to animationImages property.
For getting the various filters you can refer this project from github
GPU image open source framework for image effects.
Download the Source Code From Here:-
Hope it Helps to You :)

Applying CIFilter to UIImageView

In UIViewController displaying image in UIImageView. I want to display it with some special effects. Using core image.framework
UIImageView *myImage = [[UIImageView alloc]
initWithImage:[UIImage imageNamed:#"piano.png"]];
myImage.frame = CGRectMake(10, 50, 300, 360);
CIContext *context = [CIContext contextWithOptions:nil];
CIFilter *filter= [CIFilter filterWithName:#"CIVignette"];
CIImage *inputImage = [[CIImage alloc] initWithImage:[UIImage imageNamed:#"piano.png"]];
[filter setValue:inputImage forKey:#"inputImage"];
[filter setValue:[NSNumber numberWithFloat:18] forKey:#"inputIntensity"];
[filter setValue:[NSNumber numberWithFloat:0] forKey:#"inputRadius"];
[baseView addSubview:inputImage];
but looks like I'm missing something or doing something wrong.
As the other post indicates, a CIImage is not a view so it can't be added as one. CIImage is really only used for doing image processing, to display the filtered image you'll need to convert it back to a UIImage. To do this, you need to get the output CIImage from the filter (not the input image). If you have multiple filters chained, use the last filter in the chain. Then you'll need to convert the output CIImage to a CGImage, and from there to a UIImage. This code accomplishes these things:
CIImage *result = [filter valueForKey:kCIOutputImageKey]; //Get the processed image from the filter
CGImageRef cgImage = [context createCGImage:result fromRect:[result extent]; //Create a CGImage from the output CIImage
UIImage* outputImage = [UIImage imageWithCGImage:cgImage]; // Create a UIImage from the CGImage
Also remember that the UIImage will have to go into a UIImageView, as it's not a view itself!
For more information, see the Core Image programming guide: https://developer.apple.com/library/ios/#documentation/GraphicsImaging/Conceptual/CoreImaging/ci_intro/ci_intro.html
CIImage can not be added as subview because it is not a view (UIView subclass). You need a UIImageView with a UIImage attached to its 'image' property (and this UIImage you can create from the CIImage, I believe).

CISoftLightBlendMode not centered on Image

hey I'm having a problem with the CISoftLightBlendMode when I apply the filter to the image the overlay image is not centered over the other image it just stays in the bottom left corner so only a very small portion of the image is covered by the overlay texture.
Heres my code:
UIImage* bg = [UIImage imageNamed:#"Texture.png"];
CIImage* beginImage = [CIImage imageWithCGImage:[image_view.image CGImage]];
CIImage* bgImage = [CIImage imageWithCGImage:bg.CGImage];
context = [CIContext contextWithOptions:nil];
filter = [CIFilter filterWithName:#"CISoftLightBlendMode" keysAndValues: kCIInputImageKey, beginImage,#"inputBackgroundImage",bgImage, nil];
CIImage *outputImage = [filter outputImage];
CGImageRef cgimg = [context createCGImage:outputImage fromRect:[outputImage extent]];
UIImage *newImg = [UIImage imageWithCGImage:cgimg];
[Image_View setImage:newImg];

how to properly apply Core Image filter

Im having a trouble with Core image. what I'm doing is getting an image from a UIImageView and then use some code i found in tutorials (I'm new to core Image) but then I want to put the sepia image back into the same same UIImageView when ever I try to put the new image into the view it just disappears I have tested to see if the image view contains an image and it does but it is not visible. any suggestions on what to do?
EDIT:
okay I got the sepia filter to work so then I tried posterize and i had the same problem the image just disappears. Here is the code:
CIImage *beginImage = [CIImage imageWithCGImage:[image_view.image CGImage]];
context = [CIContext contextWithOptions:nil];
filter = [CIFilter filterWithName:#"CIColorPosterize" keysAndValues:kCIInputImageKey, beginImage,#"inputLevels",[NSNumber numberWithFloat:0.8], nil];
CIImage *outputImage = [filter outputImage];
CGImageRef cgimg = [context createCGImage:outputImage fromRect:[outputImage extent]];
UIImage *newImg = [UIImage imageWithCGImage:cgimg];
[image_view setImage:newImg];
CGImageRelease(cgimg);
Try something like this (let say you have an UIImageView *myImageView)
CIImage *beginImage = [CIImage imageWithCGImage:[myImageView.image CGImage]];
CIContext *context = [CIContext contextWithOptions:nil];
CIFilter *filter = [CIFilter filterWithName:#"CISepiaTone" keysAndValues: kCIInputImageKey, beginImage, #"inputIntensity", [NSNumber numberWithFloat:0.8], nil];
CIImage *outputImage = [filter outputImage];
CGImageRef cgimg = [context createCGImage:outputImage fromRect:[outputImage extent]];
UIImage *newImg = [UIImage imageWithCGImage:cgimg];
[myImageView setImage:newImg];
CGImageRelease(cgimg);
Same code in Swift 3.0 on Xcode 8.2 (My imageView in project is "#IBOutlet var photoImageView : UIImageView!")
Checked successfully on an iPhone 7Plus device iOS 10
guard let myImageView = self.photoImageView.image else
{
return
}
guard let cg = myImageView.cgImage else
{
return
}
let beginImage = CIImage(cgImage: cg)
let context : CIContext = CIContext(options: nil)
let inputParams : [String : Any]? = [kCIInputImageKey : beginImage,
"inputIntensity" : NSNumber(value : 0.8)]
let filter : CIFilter = CIFilter(name: "CISepiaTone", withInputParameters: inputParams)!
let outputImage : CIImage? = filter.outputImage!
if let outputImage = outputImage
{
let cgImage : CGImage? = context.createCGImage(outputImage, from: outputImage.extent)!
if let cg = cgImage
{
let newImage : UIImage = UIImage(cgImage: cg)
photoImageView.image = newImage
}
}

Simple resizing image

I've been trying to use a method commonly used to resize an image. Without using this method, here is the code that takes a url of an image.
NSData *data = [NSData dataWithContentsOfURL:url];
UIImage *img = [[UIImage alloc] initWithData:data];
cell.imageView.image = img;
This works fine. But when I try to use this method:
-(UIImage*)imageWithImage:(UIImage*)image scaledToSize:(CGSize)newSize;
{
UIGraphicsBeginImageContext( newSize );
[image drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];
UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return newImage;
}
and call it using this:
UIImage *scaledImage = [self imageWithImage:img scaledToSize:CGSizeMake(10.0f,10.0f)];
then putting into my table like this:
cell.imageView.image = scaledImage;
Nothing shows up. Is there something I'm missing here?
This is similar to Exporting customized UITableViewCells into UIImage
Here's what you need to do in your -imageWithImage:scaledToSize: method, modified from my answer to that question:
-(UIImage*)imageWithImage:(UIImage*)image scaledToSize:(CGSize)newSize
{
// Create a bitmap context.
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
CGContextRef bitmapContextForScaledImage = CGBitmapContextCreate(nil, newSize.width, newSize.height, 8, 0, colorSpace, kCGImageAlphaNone);
CGColorSpaceRelease(colorSpace);
// Draw the image's layer into the context.
UIImageView * imageView = [[UIImageView alloc] initWithImage:image];
[imageView.layer renderInContext:bitmapContextForCell];
[imageView release];
// Create a CGImage from the context.
CGImageRef cgScaledImage = CGBitmapContextCreateImage(bitmapContextForScaledImage);
// Create a UIImage from the CGImage.
UIImage * scaledImage = [UIImage imageWithCGImage:cgScaledImage];
// Clean up.
CGImageRelease(cgScaledImage);
CGContextRelease(bitmapContextForScaledImage);
return scaledImage;
}