How to set the QR code style generated by iOS? - ios

I use the following method to generate QR code:
// 1.
CIFilter *filter = [CIFilter filterWithName:#"CIQRCodeGenerator"];
//
[filter setDefaults];
// 2.
NSString *string = #"http://192.168.0.116/1";
NSData *data = [string dataUsingEncoding:NSUTF8StringEncoding];
//
[filter setValue:data forKeyPath:#"inputMessage"];
// 3.
CIImage *image = [filter outputImage];
// 4.
CGFloat scaleX = self.imageView.frame.size.width / image.extent.size.width;
CGFloat scaleY = self.imageView.frame.size.height / image.extent.size.height;
image = [image imageByApplyingTransform:CGAffineTransformMakeScale(scaleX,
scaleY)];
self.imageView.image = [UIImage imageWithCIImage:image scale:[UIScreen
mainScreen].scale orientation:UIImageOrientationUp];`
Got this picture:
But what I want is this denser two-dimensional code image like this:
How do I style it thanks very much

Related

attempt to insert nil object from objects[0]

A feature of my React Native App consists of making Perspective Corrections to Document Photos.
It takes 4 points, crops the image, perspective corrects it, and then applys CIFilter to adjust the colours and export as Base64 String.
We were trying to run this on an iPhone 11 Simulator
As of now, we have been getting this error
attempt to insert nil object from objects[0]
Assumption: It is probably happening because it can't read the image from file storage / the file is being read as nil
Here is the source code
#import "CustomCropManager.h"
#import <React/RCTLog.h>
#implementation CustomCropManager
RCT_EXPORT_MODULE();
RCT_EXPORT_METHOD(crop:(NSDictionary *)points imageUri:(NSString *)imageUri callback:(RCTResponseSenderBlock)callback)
{
NSLog(#"[myLOG] PARSING");
NSString *parsedImageUri = [imageUri stringByReplacingOccurrencesOfString:#"file://" withString:#""];
NSLog(#"[myLOG] parsedImageUri");
NSURL *fileURL = [NSURL fileURLWithPath:parsedImageUri];
NSLog(#"[myLOG] fileURL");
CIImage *ciImage = [CIImage imageWithContentsOfURL:fileURL];
NSLog(#"[myLOG] ciImage");
CGPoint newLeft = CGPointMake([points[#"topLeft"][#"x"] floatValue], [points[#"topLeft"][#"y"] floatValue]);
CGPoint newRight = CGPointMake([points[#"topRight"][#"x"] floatValue], [points[#"topRight"][#"y"] floatValue]);
CGPoint newBottomLeft = CGPointMake([points[#"bottomLeft"][#"x"] floatValue], [points[#"bottomLeft"][#"y"] floatValue]);
CGPoint newBottomRight = CGPointMake([points[#"bottomRight"][#"x"] floatValue], [points[#"bottomRight"][#"y"] floatValue]);
NSLog(#"[myLOG] CGPOINTS");
newLeft = [self cartesianForPoint:newLeft height:[points[#"height"] floatValue] ];
newRight = [self cartesianForPoint:newRight height:[points[#"height"] floatValue] ];
newBottomLeft = [self cartesianForPoint:newBottomLeft height:[points[#"height"] floatValue] ];
newBottomRight = [self cartesianForPoint:newBottomRight height:[points[#"height"] floatValue] ];
NSLog(#"[myLOG] new");
NSMutableDictionary *rectangleCoordinates = [[NSMutableDictionary alloc] init];
rectangleCoordinates[#"inputTopLeft"] = [CIVector vectorWithCGPoint:newLeft];
rectangleCoordinates[#"inputTopRight"] = [CIVector vectorWithCGPoint:newRight];
rectangleCoordinates[#"inputBottomLeft"] = [CIVector vectorWithCGPoint:newBottomLeft];
rectangleCoordinates[#"inputBottomRight"] = [CIVector vectorWithCGPoint:newBottomRight];
NSLog(#"[myLOG] rectangleCoordinates");
ciImage = [ciImage imageByApplyingFilter:#"CIPerspectiveCorrection" withInputParameters:rectangleCoordinates];
NSLog(#"[myLOG] ciImage");
// custom code
CIFilter* colorControlsFilter = [CIFilter filterWithName:#"CIColorControls"];
[colorControlsFilter setValue:ciImage forKey:#"inputImage"];
[colorControlsFilter setValue:[NSNumber numberWithFloat:1.0] forKey:#"inputSaturation"];
[colorControlsFilter setValue:[NSNumber numberWithFloat:0.2] forKey:#"inputBrightness"];
[colorControlsFilter setValue:[NSNumber numberWithFloat:1.0] forKey:#"inputContrast"];
ciImage = [colorControlsFilter valueForKey:#"outputImage"];
// custom code
NSLog(#"[myLOG] ciImage ssss");
CIContext *context = [CIContext contextWithOptions:nil];
CGImageRef cgimage = [context createCGImage:ciImage fromRect:[ciImage extent]];
UIImage *image = [UIImage imageWithCGImage:cgimage];
NSLog(#"[myLOG] image");
NSData *imageToEncode = UIImageJPEGRepresentation(image, 0.8);
NSLog(#"[myLOG] calling...");
callback(#[[NSNull null], #{#"image": [imageToEncode base64EncodedStringWithOptions:NSDataBase64Encoding64CharacterLineLength]}]);
}
- (CGPoint)cartesianForPoint:(CGPoint)point height:(float)height {
return CGPointMake(point.x, height - point.y);
}
#end
"Must have 50 reputation to comment"
You should output every variable you are trying to use. Change
NSLog(#"[myLOG] ciImage"); to NSLog(#"[myLOG] ciImage %#", ciImage);
If it prints out NSLog(#"[myLOG] ciImage"); then the crash must happen after that and before the next NSLog. Meaning this line fails:
[colorControlsFilter setValue:ciImage forKey:#"inputImage"];
As you stated the image is nil which means the line that set the image contains an error.
ciImage = [ciImage imageByApplyingFilter:#"CIPerspectiveCorrection" withInputParameters:rectangleCoordinates];
Does the filter exist and is written properly?
For debugging purposes you can uncomment this line and see if the original image displays.

IOS: AutoAdjustRedEye not working

Before Applying filter Image
After applying Red Eye filter, get bad output image
Output Image is different every time.
Here is the code of my red eye correction filter
UIImage *img = self.imageInput;
CIImage *image = [[CIImage alloc]initWithImage:img];
NSLog(#"after ciimage: %#", kCIImageAutoAdjustEnhance);
NSDictionary *options = [NSDictionary dictionaryWithObject:#"NO" forKey:kCIImageAutoAdjustEnhance];
NSLog(#"options: %#", options);
NSArray *adjustments = [image autoAdjustmentFiltersWithOptions:options];
NSLog(#"adjustments: %# ", adjustments);
for (CIFilter * filter in adjustments)
{
[filter setValue:image forKey:kCIInputImageKey];
image = filter.outputImage;
}
CIContext *context = [CIContext contextWithOptions:nil];
CGImageRef cgImage = [context createCGImage:image fromRect:image.extent];
UIImage *enhancedImage = [[UIImage alloc] initWithCGImage:cgImage];
CGImageRelease(cgImage);
self.imageViewSource.image = enhancedImage;

How do I apply a CIFilter to a UIImage?

My app is crashing when I attempt to apply a filter to my user-selected UIImage (It has been working fine without applying the filter). I added and imported the "CoreImage" framework to my project so I could create filters for user-selected images.
I am attempting to apply the filter by creating a category for UIImage (based on Apple's documentation, and then calling the corresponding method on the UIImage selected by the user. Following is the code of my category header and body; what am I doing wrong? (please note, "randColor" is a category UIColor class method to generate a random color)
#import <UIKit/UIKit.h>
#import <CoreImage/CoreImage.h>
#import "UIColor+CustomColorCategory.h"
#interface UIImage (MonoChromeFilter)
- (UIImage *) applyMonoChromeWithRandColor;
#end
#import "UIImage+MonoChromeFilter.h"
#implementation UIImage (MonoChromeFilter)
- (UIImage *)applyMonoChromeWithRandColor
{
CIContext *context = [CIContext contextWithOptions:nil];
CIImage *ciImage = [[CIImage alloc] initWithImage:self];
CIFilter *filter = [CIFilter filterWithName:#"CIColorMonochrome"];
[filter setValue:ciImage forKey:kCIInputImageKey];
[filter setValue:[UIColor randColor] forKey:kCIAttributeTypeColor];
CIImage *result = [filter valueForKey:kCIOutputImageKey];
CGRect extent = [result extent];
CGImageRef cgImage = [context createCGImage:result fromRect:extent];
UIImage *filteredImage = [[UIImage alloc] initWithCGImage:cgImage];
return filteredImage;
}
#end
Here is the method in the viewController where this category is being called:
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
[picker dismissViewControllerAnimated:YES completion:^{
UIImage *editedImage = [info objectForKey:UIImagePickerControllerEditedImage];
editedImage = [editedImage applyMonoChromeWithRandColor];
self.blogImageOutlet.image = editedImage;
self.blogImageOutlet.layer.cornerRadius = self.blogImageOutlet.frame.size.width / 2.0;
[self.blogImageOutlet setClipsToBounds:YES];
[self saveImageToLibrary:editedImage];
}];
}
I figured it out! After debugging and using some other projects as a point of reference, I realized that I was experiencing two issues. First, I was trying to use a UIColor for CIColor, which is not directly possible. I first had to covert the UIColor to a CIColor to be able to apply it. Next, I was not using the correct strings for the CIFilter value keys. Here is the following code after modifications (and now it works!)
#import "UIImage+MonoChromeFilter.h"
#implementation UIImage (MonoChromeFilter)
+ (UIImage *) applyMonoChromeWithRandColor: (UIImage *)uIImage
{
// Convert UIColor to CIColor
CGColorRef colorRef = [UIColor randColor].CGColor;
NSString *colorString = [CIColor colorWithCGColor:colorRef].stringRepresentation;
CIColor *coreColor = [CIColor colorWithString:colorString];
CIContext *context = [CIContext contextWithOptions:nil];
// Convert UIImage to CIImage
CIImage *ciImage = [[CIImage alloc] initWithImage:uIImage];
// Set values for CIColorMonochrome Filter
CIFilter *filter = [CIFilter filterWithName:#"CIColorMonochrome"];
[filter setValue:ciImage forKey:kCIInputImageKey];
[filter setValue:#1.0 forKey:#"inputIntensity"];
[filter setValue:coreColor forKey:#"inputColor"];
CIImage *result = [filter valueForKey:kCIOutputImageKey];
CGRect extent = [result extent];
CGImageRef cgImage = [context createCGImage:result fromRect:extent];
UIImage *filteredImage = [[UIImage alloc] initWithCGImage:cgImage];
return filteredImage;
}
#end
Try This , It's work like champs for me ,
We have applied different CIFilter for single image,
Below is code for implementation ,
//CISepiaTone Effect,
CIContext *imageContext = [CIContext contextWithOptions:nil];
CIImage *image = [[CIImage alloc]initWithImage:inputimage];
CIFilter *filter = [CIFilter filterWithName:#"CISepiaTone"
keysAndValues: kCIInputImageKey, image,
#"inputIntensity", #1, nil];
CIImage *result = [filter valueForKey: #"outputImage"];
CGImageRef cgImageRef = [imageContext createCGImage:result fromRect:[result extent]];
UIImage *targetImage = [UIImage imageWithCGImage:cgImageRef];
detailsVc.filterImage=targetImage;
[self.navigationController pushViewController:detailsVc animated:YES];
//CIVignette Effect
CIContext *imageContext = [CIContext contextWithOptions:nil];
CIImage *image = [[CIImage alloc] initWithImage:inputimage];
CIFilter *vignette = [CIFilter filterWithName:#"CIVignette"];
[vignette setDefaults];
[vignette setValue: image forKey: #"inputImage"];
[vignette setValue: [NSNumber numberWithFloat: 1.0] forKey: #"inputIntensity"];
[vignette setValue: [NSNumber numberWithFloat: 10.00 ] forKey: #"inputRadius"];
CIImage *result = [vignette valueForKey: #"outputImage"];
CGImageRef cgImageRef = [imageContext createCGImage:result fromRect:[result extent]];
UIImage *targetImage = [UIImage imageWithCGImage:cgImageRef];
detailsVc.filterImage=targetImage;
[self.navigationController pushViewController:detailsVc animated:YES];
For detail implementation you can refer this GitHub project file ImageFilter
Hope so This will help for some one .

CIImage memory leak

I'm using the below method to blur some images. Using instruments the CIImage's are leaking. I tried wrapping them in an #autoreleasepool, but no luck. Any ideas?
-(UIImage *)blurImage:(UIImage *)image withStrength:(float)strength
{
#autoreleasepool {
CIContext *context = [CIContext contextWithOptions:nil];
CIImage *inputImage = [[CIImage alloc] initWithCGImage:image.CGImage];
CIFilter *filter = [CIFilter filterWithName:#"CIGaussianBlur"];
[filter setValue:inputImage forKey:#"inputImage"];
[filter setValue:[NSNumber numberWithFloat:strength] forKey:#"inputRadius"];
CIImage *result = [filter valueForKey:kCIOutputImageKey];
float scale = [[UIScreen mainScreen] scale];
CIImage *cropped=[result imageByCroppingToRect:CGRectMake(0, 0, image.size.width*scale, image.size.height*scale)];
CGRect extent = [cropped extent];
CGImageRef cgImage = [context createCGImage:cropped fromRect:extent];
UIImage *returnImage = [UIImage imageWithCGImage:cgImage].copy;
CGImageRelease(cgImage);
return returnImage;
}
}
I see the same leak you're seeing when profiling the code. Try this instead which seems to avoid the leak and give you the same results:
- (UIImage*)blurImage:(UIImage*)image withStrength:(float)strength
{
#autoreleasepool {
CIImage* inputImage = [[CIImage alloc] initWithCGImage:image.CGImage];
CIFilter* filter = [CIFilter filterWithName:#"CIGaussianBlur"];
[filter setValue:inputImage forKey:#"inputImage"];
[filter setValue:[NSNumber numberWithFloat:strength] forKey:#"inputRadius"];
CIImage* result = [filter valueForKey:kCIOutputImageKey];
float scale = [[UIScreen mainScreen] scale];
CIImage* cropped = [result imageByCroppingToRect:CGRectMake(0, 0, image.size.width * scale, image.size.height * scale)];
return [[UIImage alloc] initWithCIImage:cropped];
}
}
did you try to put CIImages to nil ?
-(UIImage *)blurImage:(UIImage *)image withStrength:(float)strength
{
//your code
CGImageRelease(cgImage);
cropped=nil;
result = nil;
inputImage = nil;
context = nil;
return returnImage;
}
}

CIGaussianBlur changes imageOrientation sometime

In my iOS app I want to apply a filter CIGaussianBlur on UIImage, when it gets a image having big height it rotates the image
CIContext *context = [CIContext contextWithOptions:nil];
CIImage *inputImage = [[CIImage alloc] initWithImage:image]; //get image for blur
CIFilter *blurFilter = [CIFilter filterWithName:#"CIGaussianBlur"];
[blurFilter setDefaults];
[blurFilter setValue:inputImage forKey:#"inputImage"];
CGFloat blurLevel = 0.0f; // Set blur level
[blurFilter setValue:[NSNumber numberWithFloat:blurLevel] forKey:#"inputRadius"];
// set value for blur level
CIImage *outputImage = [blurFilter valueForKey:#"outputImage"];
CGRect rect = inputImage.extent; // Create Rect
rect.origin.x += blurLevel; // and set custom params
rect.origin.y += blurLevel; //
rect.size.height -= blurLevel*2.0f; //
rect.size.width -= blurLevel*2.0f; //
CGImageRef cgImage = [context createCGImage:outputImage fromRect:rect];
// Then apply new rect
UIImageOrientation originalOrientation = _imageView.image.imageOrientation;
CGFloat originalScale = _imageView.image.scale;
UIImage *fixedImage=[UIImage imageWithCGImage:cgImage scale:originalScale orientation:originalOrientation] ; //output of CIGaussianBlur
It works for me.
_imageView.image=image;
UIImageOrientation originalOrientation = _imageView.image.imageOrientation;
CGFloat originalScale = _imageView.image.scale;
UIImage *fixedImage=[UIImage imageWithCGImage:cgImage scale:originalScale orientation:originalOrientation] ;

Resources