Quantcast
Channel: User Bazinga - Stack Overflow
Viewing all articles
Browse latest Browse all 37

Draw in image programmatically

$
0
0

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]

Next, number 55 is picked. It adds another circle to the number.Output:

[IMG]http://i186.photobucket.com/albums/x3/arkei8105/2.jpg[/IMG]

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];        }}

Viewing all articles
Browse latest Browse all 37

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>