Control the amount of filter using GPUImageLookUpFilter with a slider - gpuimage

I want to add a slider to control the amount of filter on images. Is it possible to be done with GPUImageLookUp Filter? or is there any other way to do that using GPUImage? Here is the code that applies the filter on the image:
NSString *filename = #"lookup.png";
GPUImagePicture *lookupImageSource = [[GPUImagePicture alloc] initWithImage:[UIImage imageNamed:filename]];
GPUImageLookupFilter *lookupFilter = [[GPUImageLookupFilter alloc] init];
[stillImageSource addTarget:lookupFilter];
[lookupImageSource addTarget:lookupFilter];
[stillImageSource processImage];
[lookupImageSource processImage];
[lookupFilter useNextFrameForImageCapture];
filteredimage = [lookupFilter imageFromCurrentFramebuffer];

You want the intensity property of GPUImageLookupFilter. Varying it from 0.0 to 1.0 will change the degree to which the lookup is applied to your source image.

Related

UITextView scroll performance for large attributed strings

How can scroll performance of a UITextView be improved when using attributed strings? The following sample code results in completely unacceptable performance on an iPad 3.
NSMutableParagraphStyle *pStyle = [[NSMutableParagraphStyle alloc] init];
pStyle.lineSpacing = 14.0;
pStyle.lineHeightMultiple = 20;
pStyle.maximumLineHeight = 20.0;
pStyle.minimumLineHeight = 20.0;
UIFont *font = [UIFont systemFontOfSize:20.0];
NSDictionary *attributes = #{ NSFontAttributeName : font, NSParagraphStyleAttributeName : pStyle};
NSMutableAttributedString *newline = [[NSMutableAttributedString alloc] initWithString:#"\n\n"];
NSMutableAttributedString *string = [[NSMutableAttributedString alloc] initWithString:#"" attributes:#{}];
for (int paragraph=0; paragraph<300; paragraph++){
for (int word=0; word<100; word++){
[string appendAttributedString:[[NSAttributedString alloc] initWithString:#"text!"]];
}
[string appendAttributedString:newline];
}
[string setAttributes:attributes range:NSMakeRange(0, string.length)];
self.textView.attributedText = string;
I should mention that I am open to using Text Kit, but I wouldn't know where to start to ensure that performance is ultimately ok. Also, here's what's going on in the profiler while scrolling.
Two things help here.
First, I only needed the lineSpacing style set. Setting any of the other paragraph parameters results in slow scrolling.
The second was to call [self.textView.layoutManager ensureLayoutForCharacterRange:NSMakeRange(0, self.textView.attributedText.length)]; in viewDidAppear:. Otherwise the first time you scroll through the document it is jerky.

Query/Filter Core Data Entities with common parent whom are not key value complient

Is there a way create a predicate query to filter child entities? I would like to be able to return a single result set with filtering applied to the children.
For example I have Shape. And Circle an Square are shapes and I want to do this:
NSManagedObjectContext * context = [self managedObjectContext];
Circle * c = [NSEntityDescription insertNewObjectForEntityForName:#"Circle" inManagedObjectContext:context];
c.radius = #(10);
Square * s = [NSEntityDescription insertNewObjectForEntityForName:#"Square" inManagedObjectContext:context];
s.width = #(100);
s.height = #(200);
NSFetchRequest * f= [NSFetchRequest fetchRequestWithEntityName:#"Shape"];
f.predicate =[NSPredicate predicateWithFormat:#" radius = 10 OR hight = 200"];;
NSArray * results = [context executeFetchRequest:f error:nil];
NSLog(#"%#",results);
This produces a key value exception to be called when the fetch request is executed because a square does not have a radius, and a circle does not have a height and width.
You can only achieve this by two ways - either add all possible properties to all entities (add radius to Square and width, height to Circle) or execute two fetch requests, one for circles and one for squares. I am for the second method.
NSFetchRequest * f = [NSFetchRequest fetchRequestWithEntityName:#"Circle"];
f.predicate =[NSPredicate predicateWithFormat:#" radius = 10"];
NSArray * results = [context executeFetchRequest:f error:nil];
if(results == nil) { results = #[]; }
f = [NSFetchRequest fetchRequestWithEntityName:#"Square"];
f.predicate = [NSPredicate predicateWithFormat:#"height = 200"];
results = [results arrayByAddingObjectsFromArray:[context executeFetchRequest:f error:nil]];
I would go with denormalizing the Shape entity:
Include all properties (which is the internal implementation of CoreData in any case [for a SQLite store]) in the Shape entity and only create implementations (#dynamic) for specialised shapes (Circle, Square, ...).
You can limit this denormalization to only properties you need to search by.
Notes:
You might want to avoid setting a default value for the denormalized properties in the Shape entity, as the inheriting entities would still return value when using: [object valueForKey:#"somepropertynamefromspecializedclass"];. This way they would return nil for such keys.
You might also want to add a shapeType property to your Shape entity so you might be able to limit your search for only specific types of Shapes
This way you only need a single fetch to get the objects you want and track them with a single FRC.
Example:
Shape: [abstract entity] (shapeType,name,radius,width,height): generate implementation for this entity (Shape.h and Shape.m) but remove the implementation for properties that were denormalized (radius,width,height)
so your Shape .h file looks like:
#interface Shape : NSManagedObject
#property (nonatomic, retain) NSString * name;
#property (nonatomic, retain) NSString * shapeType;
//radius ,width and height were removed (REMEMBER TO REMOVE THEM FROM YOUR .M FILE AS WELL)
#end
Shape .m file:
#implementation Shape
#dynamic name;
#dynamic shapeType;
#end
Circle: [inherit from Shape] ()
Circle .h file:
#interface Circle : Shape
#property (nonatomic,strong) NSNumber* radius;
#end
Circle .m file:
#implementation Circle
#dynamic radius;
#end
Rectangle: [inherit from Shape] ()
implemented very similarly to Circle
and now somewhere in your code you can do:
Circle* c = (Circle*)[NSEntityDescription insertNewObjectForEntityForName:#"Circle" inManagedObjectContext:self.managedObjectContext];
c.name = #"circle";
c.shapeType = #"c";
c.radius = #(100);
Rectangle* rect = (Rectangle*)[NSEntityDescription insertNewObjectForEntityForName:#"Rectangle" inManagedObjectContext:self.managedObjectContext];
rect.name = #"rectangle";
rect.shapeType = #"r";
rect.width = #(150);
rect.height = #(200);
[self.managedObjectContext save:nil];
[self.managedObjectContext reset];
NSFetchRequest* r = [NSFetchRequest fetchRequestWithEntityName:#"Shape"];
r.predicate = [NSPredicate predicateWithFormat:#"width > %# OR radius > %#",#(100),#(90)];
NSArray* res = [self.managedObjectContext executeFetchRequest:r error:nil];

how to get string that fits to a constrained size such as UILabel

I have a very very long text, so instead of using UITextView, I want to truncate the text to small chunks that can fit to a label with width and height 300, 400, and based on number of chunks, I want to create UILabel dynamically and populate them with these chunks.
I have written a method but it seems doesn't return the actual strings that can fit. it returns shorter strings.
am I missing something?
is there anyway I can do this?
- (NSArray *)truncate:(NSString *)text
{
NSMutableArray *textChunks = [[NSMutableArray alloc] init];
NSString *chunk = [[NSString alloc] init];
CTFramesetterRef frameSetter;
UIFont *uiFont = [UIFont systemFontOfSize:17.0f];
CTFontRef ctFont = CTFontCreateWithName((__bridge CFStringRef)uiFont.fontName, uiFont.pointSize, NULL);
NSDictionary *attr = [NSDictionary dictionaryWithObject:(__bridge id)ctFont forKey:(id)kCTFontAttributeName];
NSMutableAttributedString *attrString = [[NSMutableAttributedString alloc] initWithString:text attributes:attr];
CFRange fitRange;
while (attrString.length>0) {
frameSetter = CTFramesetterCreateWithAttributedString ((__bridge CFAttributedStringRef) attrString);
CGSize framSize = CTFramesetterSuggestFrameSizeWithConstraints(frameSetter, CFRangeMake(0,0), NULL, CGSizeMake(myLabel.frame.size.width, myLabel.frame.size.height), &fitRange);
NSLog(#"height: %f", framSize.height);
CFRelease(frameSetter);
chunk = [[attrString attributedSubstringFromRange:NSMakeRange(0, fitRange.length)] string];
[textChunks addObject:chunk];
[attrString setAttributedString: [attrString attributedSubstringFromRange:NSMakeRange(fitRange.length, attrString.string.length-fitRange.length)]];
}
return textChunks;
}
Yes, you need to add a paragraph style in order to get the width/height measure to work properly. See giving-framesetter-the-correct-line-spacing-adjustment. Note how CTFontGetLeading() is used to get the font leading and then this property is set vai CFAttributedStringSetAttributes().

How to retrieve real pixel's HDR colors associated with CIImage

Can someone advice me
how to retrieve real pixel's high dynamic range(cn be > 1.f) colors from CIImage ?
This method returns data scaled between[0...1]
NSBitmapImageRep * rep = [[[NSBitmapImageRep alloc] initWithCIImage:[self ciImage]] autorelease];
return [rep colorAtX:imgPt.x y:imgPt.y];
Thank You.

Change color of button based on user input (button press)

I need to change the images/colors (whichever is easier) of buttons in a view different from the one the button is located. Any ideas? This is for iOS and I have been trying for days to get this to work. Thanks!
I don't know if this is the proper way to do this (kinda depends on the context), but you could you us user defaults:
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
NSString *fooButtonColor = #"red";
[prefs setObject:fooButtonColor forKey:#"fooButtonColor"];
You would put this code in the .m file of the class where you want to "set" the color. In
Then in the other view, get the color from the defaults and check e.g. by doing a string comparison:
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
[prefs setObject:fooButtonColor forKey:#"fooButtonColor"];
NSString *fooButtonColor = [prefs objectForkey:#"fooButtonColor"];
if ([fooButtonColor isEqualtoString #"red"]) {
foobutton.backgroundColor = [UIColor redColor];
}
Probably more efficient would be to use rgba values. Check out this question: Saving UIColor to and loading from NSUserDefaults
Good luck

Resources