我的宾果游戏应用程序遇到问题,每当在我的数字生成器中选择一个数字时,我都会尝试以编程方式画一个圆圈。
我尝试了这段代码,它在图像中绘制圆圈,然后将其保存到documentsDirectory
. 我还有一个加载实现,当我调用它时将它加载到视图中。
//画
-(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];
}
//在图片中画圆
- (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;
}
//从文档目录加载
- (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;
}
我成功地在我的图像中绘制了一个圆圈,但我的问题是当我保存带有圆圈的图像时,documentsDirectory
我希望能够加载保存的图像并再次使用该图像进行绘制。或者更确切地说,我将如何像宾果应用程序一样实现它,如下所示:
示例:首先,在数字生成器中选择数字 7。输出:
接下来,选择 55 号。它为数字添加了另一个圆圈。输出:
顺便说一句,我正在使用UIScrollview
. 我正在ScrollViewDidEndScrolling
. 谢谢。
我也试过这段代码,但每次UIScrollView
停止时它只显示一个圆圈。
- (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];
}
}