How do I make my variables accessible to other classes? - iphone

The variables bounds, width and height are at present local variables. I cannot access them from other classes or even access them from another method.
How can I make these variables available to the whole instance? I have tried placing them within the .h file and renaming them to CGFloats to no avail.
#import "TicTacToeBoard.h"
#implementation TicTacToeBoard
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
}
return self;
}
- (void)drawRect:(CGRect)rect
{
CGRect bounds = [self bounds];
float width = bounds.size.width;
float height = bounds.size.height;
CGContextRef ctx = UIGraphicsGetCurrentContext();
CGContextSetRGBStrokeColor(ctx, 0.3, 0.3, 0.3, 1);
CGContextSetLineWidth(ctx, 5);
CGContextSetLineCap(ctx, kCGLineCapRound);
CGContextMoveToPoint(ctx, width/3, height * 0.95);
CGContextAddLineToPoint(ctx, width/3, height * 0.05);
CGContextStrokePath(ctx);
}
#end

You can use properties to make variables accessible to other objects.
In your interface add something like this:
#property (nonatomic, retain) NSString *myString;
and then add
#synthesize mystring;
to your implementation.
Two methods will be created to get the property and to change it.
[myObject myString]; // returns the property
[myObject setMyString:#"new string"]; // changes the property
// alternately, you can write it this way
myObject.myString;
myObject.mystring = #"new string";
You can change the value of a property within a class using [self setMystring:#"new value"] or if you have the same variable already declared in the interface and then create a property from it you can keep using your variables within the class the way you are .
There's more info on properties in the developer docs: http://developer.apple.com/library/ios/#documentation/cocoa/conceptual/objectiveC/Chapters/ocProperties.html#//apple_ref/doc/uid/TP30001163-CH17-SW1

bounds, width and height are local variables that exist only in the context of the drawRect method.
Why don't you use:
CGRect bounds = [self bounds];
float width = bounds.size.width;
float height = bounds.size.height;
in other methods?

Make them member variables or properties and write accessors or synthesize them.
See the Objective-C language reference.

Use getters setters or generate it with a
#property(nonatomic) CGFloat width;
#synthesize width;

Related

CGImageRef change

there. How can I chage CGImageRef from another ViewController?
I have this code:
#import "ScratchableView.h"
#implementation ScratchableView
- (id)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
//init with the scratchable gray image
scratchable = [UIImage imageNamed:#"image.jpg"].CGImage;
width = CGImageGetWidth(scratchable);
height = CGImageGetHeight(scratchable);
self.opaque = NO;
CGColorSpaceRef colorspace = CGColorSpaceCreateDeviceGray();
CFMutableDataRef pixels = CFDataCreateMutable( NULL , width * height );
alphaPixels = CGBitmapContextCreate( CFDataGetMutableBytePtr( pixels ) , width , height , 8 , width , colorspace , kCGImageAlphaNone );
provider = CGDataProviderCreateWithCFData(pixels);
CGContextSetFillColorWithColor(alphaPixels, [UIColor blackColor].CGColor);
CGContextFillRect(alphaPixels, frame);
CGContextSetStrokeColorWithColor(alphaPixels, [UIColor whiteColor].CGColor);
CGContextSetLineWidth(alphaPixels, 30.0);
CGContextSetLineCap(alphaPixels, kCGLineCapSquare);
CGImageRef mask = CGImageMaskCreate(width, height, 8, 8, width, provider, nil, NO);
scratched = CGImageCreateWithMask(scratchable, mask);
CGImageRelease(mask);
CGColorSpaceRelease(colorspace);
}
return self;
}
Now From another view controller I want to change the image.jpg with image2.jpg. Please, help me, how can I do that?
I tried with app delegate, with nsstrings, but no succes. I'm beginner.
The two common ways to let one object influence another is to either provide public properties that can be set, or provide a method + parameter on your class. In both cases the object wanting to make the change needs some way to access the second object. Common ways to do that are to find it in the UINavigation viewControllers array, or though a tabBarController, through the appDelegate, or the second class is a singleton and can be found by asking the class object for the reference.
When you get this sorted out, the calling class with do something like send a message having this signature:
- (BOOL)updateImage:(CGImageRef)theNewImage

how to customize MKPolyLineView to draw different style lines

I want to customize the lines drawn on MKMapView to show a route so that the lines have a border color and a fill color. Similar to this where it has a black border and is filled with another color:
I'm currently just returning MKPolyLineView objects from mapView:viewForOverlay: which works fine for plain lines. The docs says the MKPolyLineView is not to be subclassed, so should I subclass MKOverlayView and implement my own drawMapRect? Or should I subclass MKOverlayPathView? Or create a replacement for MKPolylineView?
EDIT - what I'm asking is: where is the place to put your own Quartz drawing code in order to draw your own annotations/overlays? Currently I've created a subclass of MKOverlayView and implement my own drawMapRect:zoomScale:inContext: It's pretty easy to draw the overlay that way but is that the best solution?
You can do this by implementing your own MKOverlayPathView subclass, which draws the path twice in the map rect. Once thicker with black and once thinner on top with another colour.
I have created a simple drop-in replacement of MKPolylineView which lets you do that: ASPolylineView.
If you want to do it yourself, the two main methods that you need to implement could look like this:
- (void)drawMapRect:(MKMapRect)mapRect
zoomScale:(MKZoomScale)zoomScale
inContext:(CGContextRef)context
{
UIColor *darker = [UIColor blackColor];
CGFloat baseWidth = self.lineWidth / zoomScale;
// draw the dark colour thicker
CGContextAddPath(context, self.path);
CGContextSetStrokeColorWithColor(context, darker.CGColor);
CGContextSetLineWidth(context, baseWidth * 1.5);
CGContextSetLineCap(context, self.lineCap);
CGContextStrokePath(context);
// now draw the stroke color with the regular width
CGContextAddPath(context, self.path);
CGContextSetStrokeColorWithColor(context, self.strokeColor.CGColor);
CGContextSetLineWidth(context, baseWidth);
CGContextSetLineCap(context, self.lineCap);
CGContextStrokePath(context);
[super drawMapRect:mapRect zoomScale:zoomScale inContext:context];
}
- (void)createPath
{
// turn the polyline into a path
CGMutablePathRef path = CGPathCreateMutable();
BOOL pathIsEmpty = YES;
for (int i = 0; i < self.polyline.pointCount; i++) {
CGPoint point = [self pointForMapPoint:self.polyline.points[i]];
if (pathIsEmpty) {
CGPathMoveToPoint(path, nil, point.x, point.y);
pathIsEmpty = NO;
} else {
CGPathAddLineToPoint(path, nil, point.x, point.y);
}
}
self.path = path;
}
You can just add two MKPolyLineView objects with the same coordinates, but different thicknesses.
Add one with a lineWidth of 10 (or whatever) with strokeColor set to black.
Then add another with a lineWidth of 6 with strokeColor set to your other desired color.
You can use the same MKPolyLine for both MKPolyLineView objects.
MKPolylineView can only be used for stroking a designated path. You can use some of the properties in MKOverlayPathView to change their appearance but only some of them would apply, e.g. fillColor, strokeColor.
If you want to draw something more complex, you can use MKOverlayPathView. It is more generic and thus suited for more than just stroking paths. For drawing simple lines, the result would be identical to MKPolylineView (at least, according to the docs).
If you want to do more complex drawing, subclass MKOverlayPathView. What you're trying to do is non-trivial.
I use a subclass NamedOverlay that holds an overlay an a name:
NamedOverlay.h
#import <Foundation/Foundation.h>
#import <MapKit/MapKit.h>
#interface NamedOverlay : NSObject <MKOverlay>
#property (strong, readonly, nonatomic) NSString *name;
#property (strong, readonly, nonatomic) id<MKOverlay> overlay;
-(id)initWithOverlay:(id<MKOverlay>)overlay andName:(NSString *)name;
#end
NamedOverlay.m
#import "NamedOverlay.h"
#implementation NamedOverlay
- (id)initWithOverlay:(id<MKOverlay>)overlay andName:(NSString *)name
{
_name = name;
_overlay = overlay;
return self;
}
- (MKMapRect)boundingMapRect
{
return [_overlay boundingMapRect];
}
- (CLLocationCoordinate2D)coordinate
{
return [_overlay coordinate];
}
-(BOOL)intersectsMapRect:(MKMapRect)mapRect
{
return [_overlay intersectsMapRect:mapRect];
}
#end
and in the map controller I instantiate two overlays with different name, then in the MKMapViewDelegate I can identify which overlay I want to draw and do something like:
- (MKOverlayView *)mapView:(MKMapView *)mapView viewForOverlay:(id < MKOverlay >)overlay
{
NamedOverlay *namedOverlay = (NamedOverlay *) overlay;
MKPolyline *polyline = namedOverlay.overlay;
if ([namedOverlay.name isEqualToString:#"top"]) {
MKPolylineView *view1 = [[MKPolylineView alloc] initWithOverlay:polyline];
view1.strokeColor = [UIColor whiteColor];
view1.lineWidth = 25.0;
return view1;
} else {
MKPolylineView *view1 = [[MKPolylineView alloc] initWithOverlay:polyline];
view1.strokeColor = [UIColor blueColor];
view1.lineWidth = 15.0;
return view1;
}
}
I know that this may not match the pure approach you want, but why not using MKPolygon instead of a MKPolyLine ?
Create a MKPolygon instance that represents a kind of corridor around your route, and then , when you create the MKPolygonView that corresponds to the MKPolygon/corridor you've created, set the properties of the MKPolygonView to get a different fill color and strokeColor
myPolygonView.lineWidth=3;
myPolygonView.fillColor=[UIColor blueColor];
myPolygonView.strokeColor=[UIColor darkGrayColor];
I didn't try it myself but this should work. Only drawback is that when you zoom in / out, the 'width' of the 'route' will change.... :/

quartz masks in iOS -- do they still cause crashes?

According to this question from 2008, using quartz masks can cause crashes! Is that still the case?
Basically, what I want to do is to draw dice of different colors on a fixed background, using one png for each die shape (there are a lot of them), and somehow add the colors in code.
EDIT: to clarify, for example I want to use one png file to make all of the following:
Basically, I want to multiply the red, green, and blue components of my image by three independent constants, while leaving the alpha unchanged.
Here's a shot. Tested, no leaks. No crashes.
.h
#import <UIKit/UIKit.h>
#interface ImageModViewController : UIViewController {
}
#property (nonatomic, retain) IBOutlet UIButton *test_button;
#property (nonatomic, retain) IBOutlet UIImageView *source_image;
#property (nonatomic, retain) IBOutlet UIImageView *destination_image;
#property (nonatomic, assign) float kr;
#property (nonatomic, assign) float kg;
#property (nonatomic, assign) float kb;
#property (nonatomic, assign) float ka;
-(IBAction)touched_test_button:(id)sender;
-(UIImage *) MultiplyImagePixelsByRGBA:(UIImage *)source kr:(float)red_k kg:(float)green_k kb:(float)blue_k ka:(float)alpha_k;
#end
.m
#define BITS_PER_WORD 32
#define BITS_PER_CHANNEL 8
#define COLOR_CHANNELS 4
#define BYTES_PER_PIXEL BITS_PER_WORD / BITS_PER_CHANNEL
#import "ImageModViewController.h"
#implementation ImageModViewController
#synthesize test_button;
#synthesize source_image;
#synthesize destination_image;
#synthesize kr;
#synthesize kg;
#synthesize kb;
#synthesize ka;
-(IBAction)touched_test_button:(id)sender
{
// Setup coefficients
kr = 1.0;
kg = 0.0;
kb = 0.0;
ka = 1.0;
// Set UIImageView image to the result of multiplying the pixels by the coefficients
destination_image.image = [self MultiplyImagePixelsByRGBA:source_image.image kr:kr kg:kg kb:kb ka:ka];
}
-(UIImage *) MultiplyImagePixelsByRGBA:(UIImage *)source kr:(float)red_k kg:(float)green_k kb:(float)blue_k ka:(float)alpha_k
{
// Get image information
CGImageRef bitmap = [source CGImage];
int width = source.size.width;
int height = source.size.height;
int total_pixels = width * height;
// Allocate a buffer
unsigned char *buffer = malloc(total_pixels * COLOR_CHANNELS);
// Copy image data to buffer
CGColorSpaceRef cs = CGColorSpaceCreateDeviceRGB();
CGContextRef context = CGBitmapContextCreate(buffer, width, height, BITS_PER_CHANNEL, width * BYTES_PER_PIXEL, cs, kCGImageAlphaPremultipliedLast | kCGBitmapByteOrderDefault);
CGColorSpaceRelease(cs);
CGContextDrawImage(context, CGRectMake(0, 0, width, height), bitmap);
CGContextRelease(context);
// Bounds limit coefficients
kr = ((((kr < 0.0) ? 0.0 : kr) > 1.0) ? 1.0 : kr);
kg = ((((kg < 0.0) ? 0.0 : kg) > 1.0) ? 1.0 : kg);
kb = ((((kb < 0.0) ? 0.0 : kb) > 1.0) ? 1.0 : kb);
ka = ((((ka < 0.0) ? 0.0 : ka) > 1.0) ? 1.0 : ka);
// Process the image in the buffer
int offset = 0; // Used to index into the buffer
for (int i = 0 ; i < total_pixels; i++)
{
buffer[offset] = (char)(buffer[offset] * red_k); offset++;
buffer[offset] = (char)(buffer[offset] * green_k); offset++;
buffer[offset] = (char)(buffer[offset] * blue_k); offset++;
buffer[offset] = (char)(buffer[offset] * alpha_k); offset++;
}
// Put the image back into a UIImage
context = CGBitmapContextCreate(buffer, width, height, BITS_PER_CHANNEL, width * BYTES_PER_PIXEL, cs, kCGImageAlphaPremultipliedLast | kCGBitmapByteOrderDefault);
bitmap = CGBitmapContextCreateImage(context);
UIImage *output = [UIImage imageWithCGImage:bitmap];
CGContextRelease(context);
free(buffer);
return output;
}
- (void)dealloc
{
[super dealloc];
}
- (void)didReceiveMemoryWarning
{
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
#pragma mark - View lifecycle
/*
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad
{
[super viewDidLoad];
}
*/
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
#end
I setup the xib with two UIImageViews and one UIButton. The top UIImageView was pre-loaded with an image with Interface Builder. Touch the text button and the image is processed and set to the second UIImageView.
BTW, I had a little trouble with your icons copied right off your post. The transparency didn't work very well for some reason. I used fresh PNG test images of my own created in Photoshop with and without transparency and it all worked as advertised.
What you do inside the loop is to be modified per your needs, of course.
Watch endianess, it can really mess things up!
I've used masks fairly extensively recently in an iPhone app with no crashes. The code in that link doesn't even seem to be using masks, just clipping; the only mention of masks was as something else he tried. More likely he was calling that from a background thread, UIGraphicsBeginImageContext isn't thread safe.
Without knowing exactly what effect you're trying to get, it's hard to give advice on how to do it. A mask certainly could work, either alone (to get a sort of silkscreened effect) or to clip an overlay color drawn on a more realistic image. I'd probably use a mask or a path to set the clipping, then draw the die image (using kCBGlendModeNormal or kCBGlendModeCopy), and then paint the appropriate solid color over it using kCGBlendModeColor.

Inheriting UIView - Losing instance variables

Bit of an Objective-C rookie, I've looked around for an answer but haven't been able to find one so forgive me if this is an obvious question.
Basically, I need to draw on the screen segments of a circle (for instance, a 90 degree segment, where a horizontal and vertical line meet at the bottom left and an arc connects the end points). I've managed to achieve this in a custom class called CircleSegment that inherits UIView and overrides drawRect.
My issue is achieving this programatically; I need some way of creating my CircleSegment class and storing in it the desired angle before it draws the segment itself.
Here's what I have so far:
CircleSegment.h
#import <UIKit/UIKit.h>
#interface CircleSegment : UIView {
float angleSize;
UIColor *backColor;
}
-(float)convertDegToRad:(float)degrees;
-(float)convertRadToDeg:(float)radians;
#property (nonatomic) float angleSize;
#property (nonatomic, retain) UIColor *backColor;
#end
CircleSegment.m
#import "CircleSegment.h"
#implementation CircleSegment
#synthesize angleSize;
#synthesize backColor;
// INITIALISATION OVERRIDES
// ------------------------
- (id)initWithFrame:(CGRect)frame {
if ((self = [super initWithFrame:frame])) {
self.opaque = NO;
self.backgroundColor = [UIColor clearColor];
}
return self;
}
- (void)setBackgroundColor:(UIColor *)newBGColor
{
// Ignore.
}
- (void)setOpaque:(BOOL)newIsOpaque
{
// Ignore.
}
// MATH FUNCTIONS
// --------------
// Converts degrees to radians.
-(float)convertDegToRad:(float)degrees {
return degrees * M_PI / 180;
}
// Converts radians to degrees.
-(float)convertRadToDeg:(float)radians {
return radians * 180 / M_PI;
}
// DRAWING CODE
// ------------
- (void)drawRect:(CGRect)rect {
float endAngle = 360 - angleSize;
UIBezierPath* aPath = [UIBezierPath bezierPathWithArcCenter:CGPointMake(100, 100)
radius:100
startAngle:[self convertDegToRad:270]
endAngle:[self convertDegToRad:endAngle]
clockwise:YES];
[aPath addLineToPoint:CGPointMake(100.0, 100.0)];
[aPath closePath];
CGContextRef aRef = UIGraphicsGetCurrentContext();
CGContextSetFillColorWithColor(aRef, backColor.CGColor);
CGContextSaveGState(aRef);
aPath.lineWidth = 1;
[aPath fill];
[aPath stroke];
//CGContextRestoreGState(aRef);
}
- (void)dealloc {
[super dealloc];
}
#end
Note that the .m file is a bit messy with various bits of test code...
So essentially I want to create an instance of CircleSegment, store an angle in the angleSize property, draw a segment based on that angle and then add that view to the main app view to display it...
To try and achieve that, I've add the following test code to viewDidLoad on my ViewController:
CircleSegment *seg1 = [[CircleSegment alloc] init];
seg1.backColor = [UIColor greenColor];
seg1.angleSize = 10;
[self.view addSubview:seg1];
It seems to store the UIColor and angleSize fine when I breakpoint those places, however if I place a breakpoint in drawRect which I overrode on CircleSegment.m, the values have reverted back to nil values (or whatever the correct term would be, feel free to correct me).
I'd really appreciate it if someone could point me in the right direction!
Thanks
It seems to store the UIColor and
angleSize fine when I breakpoint those
places, however if I place a
breakpoint in drawRect which I
overrode on CircleSegment.m, the
values have reverted back to nil
values (or whatever the correct term
would be, feel free to correct me).
Are you sure you are working with the same instance?
Drop in something like NSLog(#"%# %p", NSStringFromSelector(_cmd), self); in viewDidLoad and in drawRect and make sure you are actually dealing with the same instance.
(I've seen cases where a developer will alloc/initWithFrame: a view and then use the instance that was created during NIB loading, wondering why that instance wasn't properly initialized.)
Try using self.angleSize in your DrawRect method (same with backColor). In your test method, replace 10 with 10.0 (to ensure it's setting it as a float).
Also, don't forget to release backColor in your dealloc.

Drawing Pixels - Objective-C/Cocoa

I am trying to draw individual pixels in xcode to be outputted to the iphone. I do not know any OpenGL or Quartz coding but I do know a bit about Core Graphics. I was thinking about drawing small rectangles with width and height of one, but do not know how to implement this into code and how to get this to show in the view. Any help is greatly appreciated.
For a custom UIView subclass that allows plotting dots of a fixed size and color:
// Make a UIView subclass
#interface PlotView : UIView
#property (nonatomic) CGContextRef context;
#property (nonatomic) CGLayerRef drawingLayer; // this is the drawing surface
- (void) plotPoint:(CGPoint) point; //public method for plotting
- (void) clear; // erases drawing surface
#end
// implementation
#define kDrawingColor ([UIColor yellowColor].CGColor)
#define kLineWeight (1.5)
#implementation PlotView
#synthesize context = _context, drawingLayer = _drawingLayer;
- (id) initPlotViewWithFrame:(CGRect) frame; {
self = [super initWithFrame:frame];
if (self) {
// this is total boilerplate, it rarely needs to change
self.backgroundColor = [UIColor clearColor];
CGColorSpaceRef colorspace = CGColorSpaceCreateDeviceRGB();
CGFloat width = frame.size.width;
CGFloat height = frame.size.height;
size_t bitsPerComponent = 8;
size_t bytesPerRow = (4 * width);
self.context = CGBitmapContextCreate(NULL, width, height, bitsPerComponent, bytesPerRow, colorspace, kCGImageAlphaPremultipliedFirst);
CGColorSpaceRelease(colorspace);
CGSize size = frame.size;
self.drawingLayer = CGLayerCreateWithContext(self.context, size, NULL);
}
return self;
}
// override drawRect to put drawing surface onto screen
// you don't actually call this directly, the system will call it
- (void) drawRect:(CGRect) rect; {
// this creates a new blank image, then gets the surface you've drawn on, and stamps it down
// at some point, the hardware will render this onto the screen
CGContextRef currentContext = UIGraphicsGetCurrentContext();
CGImageRef image = CGBitmapContextCreateImage(self.context);
CGRect bounds = [self bounds];
CGContextDrawImage(currentContext, bounds, image);
CGImageRelease(image);
CGContextDrawLayerInRect(currentContext, bounds, self.drawingLayer);
}
// simulate plotting dots by drawing a very short line with rounded ends
// if you need to draw some other kind of shape, study this part, along with the docs
- (void) plotPoint:(CGPoint) point; {
CGContextRef layerContext = CGLayerGetContext(self.drawingLayer); // get ready to draw on your drawing surface
// prepare to draw
CGContextSetLineWidth(layerContext, kLineWeight);
CGContextSetLineCap(layerContext, kCGLineCapRound);
CGContextSetStrokeColorWithColor(layerContext, kDrawingColor);
// draw onto surface by building a path, then stroking it
CGContextBeginPath(layerContext); // start
CGFloat x = point.x;
CGFloat y = point.y;
CGContextMoveToPoint(layerContext, x, y);
CGContextAddLineToPoint(layerContext, x, y);
CGContextStrokePath(layerContext); // finish
[self setNeedsDisplay]; // this tells system to call drawRect at a time of it's choosing
}
- (void) clear; {
CGContextClearRect(CGLayerGetContext(self.drawingLayer), [self bounds]);
[self setNeedsDisplay];
}
// teardown
- (void) dealloc; {
CGContextRelease(_context);
CGLayerRelease(_drawingLayer);
[super dealloc];
}
If you want to be able to draw pixels that are cumulatively added to some previously drawn pixels, then you will need to create your own bitmap graphics context, backed by your own bitmap memory. You can then set individual pixels in the bitmap memory, or draw short lines or small rectangles in your graphics context. To display your drawing context, first convert it to an CGImageRef. Then you can either draw this image to a subclassed UIView in the view's drawRect, or assign the image to the contents of the UIView's CALayer.
Look up: CGBitmapContextCreate and CGBitmapContextCreateImage in Apple's documentation.
ADDED:
I wrote up a longer explanation of why you might need to do this when drawing pixels in an iOS app, plus some source code snippets, on my blog: http://www.musingpaw.com/2012/04/drawing-in-ios-apps.html
All drawing needs to go into the - (void)drawRect:(CGRect)rect method. [self setNeedsDisplay] flags the code for a redraw. Problem is your redrawing nothing.