xcode5: How to use the built in Core Image filters? - core-image

I'm new in developing iOS Apps.
I would like to use the built in core image filters in my app. But I don't know how I can use them. I have already created the "CISephiaTon" filter. Now I would like to create the "CIPhotoEffectTransfer" filter. But I don't know how I can do this =(
Does anybody knows a good tutorial or can give me one for applying core image filters in xcode 5?
This is the filter which I want to add. Could someone give me the code of this filter?
Hope someone can help me.

Observe the following code...
//First create a CIImage
UIImageOrientation originalOrientation = origim.imageOrientation;
data3 = UIImagePNGRepresentation(origim);
CIImage *result = [CIImage imageWithData:data3];
//Create the context
CIContext *context = [CIContext contextWithOptions:nil];
Apply the filter
CIFilter *colorControlsFilter = [CIFilter filterWithName:#"CIColorControls"];
[colorControlsFilter setDefaults];
[colorControlsFilter setValue:result forKey:#"inputImage"];
[colorControlsFilter setValue:[NSNumber numberWithFloat:slySlider1.value] forKey:#"inputSaturation"];
result = [colorControlsFilter valueForKey: #"outputImage"];
//Create CGImage with the original orientation, CIImage, and context.
CGImageRef imgRef = [context createCGImage:result fromRect:result.extent];
//Create the new UIImage from CGImage
theimage.image = [[UIImage alloc] initWithCGImage:imgRef scale:1.0 orientation:originalOrientation];
//Release CGImageRef
CGImageRelease(imgRef);
Good tutorial Beginning Core Image

Related

Objective-C Scenekit: Import OBJ-File externally and color it

I am new to this. And there comes my first problem.
I build an object loader with SceneKit. I got the path, the Object is available, but I have no clue how I can color the shown object.
ScnScene *testScene = [SCNScene sceneWithURL:url options:nil error:NULL];
testScene.background.contents = [UIImage imageWithName:#"color.png"];
[self.mainView.scene.rootnode addChildNode:testScene.rootNode];
This didn't work. I also tried with:
SCNMaterialProperty *testColor = [SCNMaterialProperty materialPropertyWithContents [UIImage imageNamed:#"color.png"]];
testScene.rootnode.geometry.materials = testcolor;
Or:
SCNMaterial *testColor = [SCNMaterial material];
testColor.diffuse.contesnts = [UIColor redColor];
testScene.rootnode.geometry.firstMaterial = testColor;
Nothing works. When I start the app, every object is displayed. So far the OBJ-Loader works out very well. But everything is still gray. I totally have no clue how to color the displayed objects. Has anyone a hint/idea/solution for me?
Btw. I want to avoid that I have to build a geometry out of the OBJ-Informations manually. So I try to solve this by the SCNScene.
The major problem was that the import by a SCNScenedoes not work that way. So the right solution is to import the obj.file into a SCNNode, add a SCNMaterial with the chosen color (or an image) to the SCNNode and give the SCNNodeto the SCNScene. To load the obj.file, you need to import that by Model IO Framework.
I will give some code how I made it colorful.
#import <SceneKit/SceneKit.h>
#import <ModelIO/ModelIO.h>
#import <SceneKit/ModelIO.h>
...
#property (nonatomic) SCNView* mainView;
....
MDLAsset *asset = [[MDLAsset alloc] initWithURL:url];
SCNScene *scene = [SCNScene scene];
SCNNode *node = [SCNNode nodeWithMDLObject:[asset objectAtIndex:0]];
SCNMaterial *material = [SCNMaterial material];
material.diffuse.contents = [UIColor colorWithHue:0 saturation:0.1 brightness:0.5 alpha:1];
node.geometry.firstMaterial = material;
[scene.rootNode addChildNode:node];
[self.mainView.scene.rootNode addChildNode:scene.rootNode];
Alternately you can add a color by this:
material.diffuse.contents = [UIImage imageNamed:#"farbe.png"];
Now you can import any obj.file externally (from any chosen folder you like) and color it.
Thanks to SGlindemann, cashmash and Hal Mueller, who helped out to find that solution.
UPDATE (29.1.2017)
Somehow the way above does not work anymore. I did not figure out yet what changed. But I made another code that makes loading for 3D files possible (from mainBundle, not externally). Here I start from a SCNNode class which is called by the ViewController.m. The SCNScene is setup in the ViewController. Following there is the code which I wrote for the SCNNode class.
Before you start, put the .obj and .mtl file (both with same name) into your Xcode project. You don't need to convert it to a scene.
#import <ModelIO/ModelIO.h>
#import <SceneKit/ModelIO.h>
...
#property (nonatomic) SCNNode *objectNode;
...
NSString* path = [[NSBundle mainBundle]
pathForResource:[NSString stringWithFormat:#"name of the obj.file"]
ofType:#"obj"];
NSURL *url = [NSURL fileURLWithPath:path];
MDLAsset *asset = [[MDLAsset alloc]initWithURL:url];
// Create the Block
self.objectNode = [SCNNode nodeWithMDLObject:[asset objectAtIndex:0]];
[self addChildNode: self.objectNode];
return self;
This returned self has to be added into your view.
[self.view.scene.rootNode addChildNode:returnedObj];
The MDLAssetloads the .obj-file with the corresponding .mtl-file and png-File. I used this code to load objects from MagicaVoxel (this exports obj+mtl+png at once). I did not get deep into it yet.
I did NOT try this code with external loadings or typing in colors manually via SCNMaterial. So there is no statement whether this works or not. I did not try.
Your first example will set the background of the scene but do nothing with your object.
Your second example should be giving you some compiler warnings. You're assigning a single SCNMaterialProperty to testScene.rootnode.geometry.materials, which expects an array of SCNMaterial (not SCNMaterialProperty). Is that your real code?
The final example shouldn't compile at all: you've misspelled contents as contesnts. Other than that, it ought to work.
Note that MDLAsset can import an OBJ file and return an SCNNode. See How do you convert Wavefront OBJ file to an SCNNode with Model I/O. If the object is an asset you'll be shipping with your project, save it as an SCNScene (which is compact and optimized), and ship that, not the original OBJ.

How to overlay MKPolyline on top of different maps

So I draw a MKPolyline with the users updated CLLocationCoordinates as per usual. When my quits and starts again, all of the past points are loaded onto the mapview to be displayed with
routeLine = [MKPolyline polylineWithCoordinates:clCoordinates count:numberOfSteps];
[_mapView addOverlay:routeLine];
and in MKOverlayRenderer method I have
if ([overlay isKindOfClass:[MKPolyline class]])
{
MKPolylineRenderer *renderer = [[MKPolylineRenderer alloc] initWithPolyline:overlay];
renderer.strokeColor = [[UIColor whiteColor] colorWithAlphaComponent:0.7];
renderer.lineWidth = 3;
return renderer;
}
This all works a treat, but problems arise when I change the type of map with
[self resetMapViewAndRasterOverlayDefaults];
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES];
_rasterOverlay = [[MBXRasterTileOverlay alloc] initWithMapID:#"jonobolitho.j834jdml"];
_rasterOverlay.delegate = self;
[_mapView addOverlay:_rasterOverlay];
Note, I have the resetMapViewAndRasterOverlayDefaults helper method
// Reset the MKMapView to some reasonable defaults.
//
_mapView.mapType = MKMapTypeStandard;
_mapView.zoomEnabled = YES;
[_mapView removeAnnotations:_rasterOverlay.markers];
[_mapView removeOverlay:_rasterOverlay];
[_rasterOverlay invalidateAndCancel];
I am using MBXMapKit to display the maps. The maps display correctly, but the polyline isn't shown on top of the map. The map just loads over the top of my polyline (routeLine). I know this because in between the tiles of the new map I can see the routeLine underneath.
So basically is there a way to load the MKPolyline but always keep it on the top layer of the map, even if it changes?
Instead of:
[_mapView addOverlay:_rasterOverlay];
You want:
[_mapView insertOverlay:_rasterOverlay belowOverlay:routeLine];
Or something similar. Overlays have an order.

iOS UITabBarItem setFinishedSelectedImage:withFinishedUnselectedImage: memory leak?

I have a memory leak situation about UITabBarItem. My app has a TabBarController with customized pictures. so here is the code I put on my graph files.
UIImage *selectedImage0 = [UIImage imageNamed:#"A-click.png"];
UIImage *unselectedImage0 = [UIImage imageNamed:#"A.png"];
UIImage *selectedImage1 = [UIImage imageNamed:#"B-click.png"];
UIImage *unselectedImage1 = [UIImage imageNamed:#"B.png"];
UITabBar *tabBar = self.mytabbarcontroller.tabBar;
UITabBarItem *item0 = [tabBar.items objectAtIndex:0];
UITabBarItem *item1 = [tabBar.items objectAtIndex:1];
[item0 setFinishedSelectedImage:selectedImage0 withFinishedUnselectedImage:unselectedImage0];
[item1 setFinishedSelectedImage:selectedImage1 withFinishedUnselectedImage:unselectedImage1];
And it works totally well !!!
However, when I use Instruments tool to check memory leak, it shows there is leak from
[item0 setFinishedSelectedImage:selectedImage0 withFinishedUnselectedImage:unselectedImage0];
[item1 setFinishedSelectedImage:selectedImage1 withFinishedUnselectedImage:unselectedImage1];
If I comment these two lines without using setFinishedSelectedImage, there won't be leak. I feel it is weird and doesn't make sense for me. Already searched the document and reference and couldn't find relative information. I am using iOS 6 and Xcode 4.5 . Do anyone knows about this? thanks in advance.

Cocos2d: is there a way to access to a CCMenu child?

I am using the following code:
CCMenuItemFont *controllerItem = [CCMenuItemFont itemFromString:#"Analog" target:self selector:#selector(controllerToggle)];
CCMenu *controllerTypeMenu = [CCMenu menuWithItems:controllerItem, nil];
[controllerTypeMenu alignItemsVerticallyWithPadding:30.0f];
controllerTypeMenu.position = CGPointMake(160.0f, 240.0f);
[self addChild:controllerTypeMenu z:0 tag:ControllerMenu];
}
-(void) controllerToggle
{
CCMenuItemFont *controllerItem = [self getChildByTag:888];
NSString * text = [NSString stringWithFormat: #"switching.. %f", CCRANDOM_0_1()];
[controllerItem setString:text];
}
In controllerToggle I would like to access to controllerItem and change the String to another value. Is this possible? I checked and CCMenu adds as child the CCMenuItems based the array order. But that's not an elegant solution. For the same reason I cannot add the CCMenuItem to the Scene as it will give me "child already added error". So I feel I should write my own "switch" button but I wonder if there is already something out there..
Any help?? Thanks!
you can declare needed menu item as class member. In this way you will be able to get access to it whenever you want
There is already CCMenuItemToogle which is used to switching the title or image of any menuitem on click event.Instead of taking CCMenuItem you can use CCMenuItemToggle..
Here is a sample code
CCmenuItemToggle *cam=[CCMenuItemToggle itemWithTarget:self selector:#selector(openCameraController) items:cameraOn,cameraOff, nil];
where cameraOn and cameraOff are two simple CCMenuItemImage which is added in CCMenuItemToggle and further this CCmenuItemToggle is added in a menu.
Now on every click of CCMenuItemToggle there will be a cameraOn and cameraOff state for same menuitem.
Hope this will help...

Custom Map Annotation Callout image

I'm able to set the MKPinAnnotationView with title, subtitle, an image on the left (leftCalloutAccessoryView) and a button on the right (rightCalloutAccessoryView).
With this code i set the image on the left:
UIImageView *myImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"Icon.png"]];
myImageView.frame = CGRectMake(0, 0, 10, 10);
customPinView.leftCalloutAccessoryView = myImageView;
[myImageView release], myImageView = nil;
The result is something like this:
Image 1
but I would like to get something like this:
Image 2
where the image is aligned with the subtitle.
How can get it?
Thx.
That's going to require using a custom annotationView. There's plenty of documentation about this in the developer resources. There's no way to do that image alignment using the default annotation style.

Resources