I’m having trouble with my bingo application,I tried drawing a circle programmatically whenever a number is picked in my number generator.
I tried this code, that draws the circle in image then save it to the documentsDirectory. I also have a load implementation that I load it in the view when I call it.
//draw
-(void)draw
{
UIImage *image = [UIImage imageNamed:@"GeneralBingoResult.png"];
UIImage *imageWithCircle1 = [self imageByDrawingCircleOnImage1:image];
// save it to documents
NSString *documentsPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
NSUserDomainMask, YES) lastObject];
NSString *filePath = [documentsPath stringByAppendingPathComponent:@"Output.png"];
NSData *imageData = UIImagePNGRepresentation(imageWithCircle1);
[imageData writeToFile:filePath atomically:YES];
NSLog(@"Saved new image to %@", filePath);
UIImage *image1 = [self loadImage];
[imageToDisplay setImage:image1];
}
//draws the circle in the image
- (UIImage *)imageByDrawingCircleOnImage1:(UIImage *)image
{
// begin a graphics context of sufficient size
UIGraphicsBeginImageContext(image.size);
// draw original image into the context
[image drawAtPoint:CGPointZero];
// get the context for CoreGraphics
CGContextRef ctx = UIGraphicsGetCurrentContext();
// set stroking color and draw circle
[[UIColor redColor] setStroke];
// make circle rect 5 px from border
CGRect circleRect = CGRectMake(420,40,
90,
90);
circleRect = CGRectInset(circleRect, 5, 5);
// draw circle
CGContextStrokeEllipseInRect(ctx, circleRect);
// make image out of bitmap context
UIImage *retImage = UIGraphicsGetImageFromCurrentImageContext();
// free the context
UIGraphicsEndImageContext();
return retImage;
}
//loads from doc directory
- (UIImage*)loadImage
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString* path = [documentsDirectory stringByAppendingPathComponent:
[NSString stringWithString: @"Output.png"] ];
UIImage* image = [UIImage imageWithContentsOfFile:path];
return image;
}
I successfully draw a circle in my image, but my problem is when I save the image with the circle in documentsDirectory I want to be able to load the saved image and draw with that image again. Or rather how am I going to implement it like a bingo app, like this:
Example:
First,Number 7 is picked in the number generator.
Output:
![[IMG]http://i186.photobucket.com/albums/x3/arkei8105/1-1.jpg[/IMG]](https://i.stack.imgur.com/wSJcI.jpg)
Next, number 55 is picked. It adds another circle to the number.
Output:
![[IMG]http://i186.photobucket.com/albums/x3/arkei8105/2.jpg[/IMG]](https://i.stack.imgur.com/CjLcv.jpg)
By the way, I’m using a UIScrollview. And I am implementing it in the ScrollViewDidEndScrolling. Thanks.
I also tried this code, but it only shows one circle every time the UIScrollView stops.
- (void) scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollview{
if ([[images objectAtIndex:index] intValue] == 1){
[circle setFrame:CGRectMake(420,40,90,90)];
[self.view addSubview:circle];
}else if([[images objectAtIndex:index] intValue] == 2){
[circle setFrame:CGRectMake(460,40,90,90)];
[self.view addSubview:circle];
}
}
Your easiest option is to create an image in photoshop or other similar program which is the required size and which has a transparent background. Save this as a .png file. Then, when you want to add the circles over the board, you just have to add a new UIImageView over the correct location in the grid:
An alternate option is to create a view that inherits from UIView. You can add methods to this class which allow you to set locations as circled. Then inside the drawRect code you just draw circles over all the locations which have been marked.
The final step in this case is to add your new view over your original view which contains the BINGO board.
EDIT:
Here is extended sample code for doing a view overlay. First, OverlayView.h:
Note that I use the C++ vector here, so this is in OverlayView.mm file:
You just need to add this view inside the same view as your board. Call the addPoint function to add a circle centered at that point. You’ll need to defined the resolution and width of your view for yourself.