从头开始创建新图像

今天看了这个程序    下面是我对这个程序中代码的理解

如果有那个地方我理解的不太到位   还请赐教  

1.让图片随即在什么地方出现

 

- (CGPoint) randomCenterInView: (UIView *) aView withInsets: (UIEdgeInsets) insets
{
	CGRect innerRect = UIEdgeInsetsInsetRect([aView bounds], insets);  
    
    //CGRect结构在屏幕上定义了一个矩形。 调整矩形
    //bounds的坐标原点是(0,0) 但这个点不是指左上角的那个原点 而是相对于自己 
    //但frame的原点坐标是相对于父视图的 原点坐标不一定是(0,0);
    
	CGRect subRect = CGRectInset(innerRect, self.frame.size.width / 2.0f, self.frame.size.height / 2.0f);
	
    //floor取整数
    
	float rx = (float)(random() % (int)floor(subRect.size.width));  
	float ry = (float)(random() % (int)floor(subRect.size.height));
	return CGPointMake(rx + subRect.origin.x, ry + subRect.origin.y);
}
- (CGPoint) randomCenterInView: (UIView *) aView withInset: (float) inset
{
    //在本程序中UIEdgeInsetsMake指的是矩形的四角有多少不被利用
    
	UIEdgeInsets insets = UIEdgeInsetsMake(inset, inset, inset, inset);  
	return [self randomCenterInView:aView withInsets:insets];
}

 

#define COOKBOOK_PURPLE_COLOR	[UIColor colorWithRed:0.20392f green:0.19607f blue:0.61176f alpha:1.0f]
#define BARBUTTON(TITLE, SELECTOR) [[[UIBarButtonItem alloc] initWithTitle:TITLE style:UIBarButtonItemStylePlain target:self action:SELECTOR] autorelease]
void centerText(CGContextRef context, NSString *fontname, float textsize, NSString *text, CGPoint point, UIColor *color)
{
    //使用Quartz时涉及到一个图形上下文,其中图形上下文中包含一个保存过的图形状态堆栈。
    //在Quartz创建图形上下文时,该堆栈是空的。CGContextSaveGState函数的作用是将
    //当前图形状态推入堆栈。之后,您对图形状态所做的修改会影响随后的描画操作,但不影响
    //存储在堆栈中的拷贝。在修改完成后,您可以通过CGContextRestoreGState函数把堆
    //栈顶部的状态弹出,返回到之前的图形状态。这种推入和弹出的方式是回到之前图形状态的
    //快速方法,避免逐个撤消所有的状态修改;这也是将某些状态(比如裁剪路径)恢复到原有设置的唯一方式。
    
    
	CGContextSaveGState(context);
    
	CGContextSelectFont(context, [fontname UTF8String], textsize, kCGEncodingMacRoman);
    
	//CGContextSelectFont函数有4个参数:一个图形上下文,
    //字体的PostScript字体名,字体大小(用户空间单位),以及文本的编码。
    
	//检索文本宽度
	CGContextSaveGState(context);
    
	CGContextSetTextDrawingMode(context, kCGTextInvisible); 
    
    //调用CGContextSetTextDrawingMode函数将文本设置为kCGTextInvisible。
    
	CGContextShowTextAtPoint(context, 0.0f, 0.0f, [text UTF8String], text.length);  
    
    //与上一句话一起删除的话 会呈现和不删前同样的效果
    //在绘制文本时,调用CGContextShowTextAtPoint函数。你调用CGContextSetFont
    //而不是CGContextSelectFont来指定字体,在CGContextSetFont中你并没有指定文本编码,
    //你也就不能使用CGContextShowTextAtPoint来绘制文本。
    
	CGPoint endpoint = CGContextGetTextPosition(context); 
    
    //调用CGContextGetTextPosition得到文本的结束位置。??????????????
    
	CGContextRestoreGState(context);
	
	CGSize stringSize = [text sizeWithFont:[UIFont fontWithName:fontname size:textsize]];
	
	//描绘数字
    
	[color setFill];  
	CGContextSetShouldAntialias(context, true);
    
    //让字体渲染比较清晰 提高画质以使之柔和
    
	CGContextSetTextDrawingMode(context, kCGTextFill);
    
    //kCGTextClip kCGTextInvisible这样中间没数字 kCGTextFill kCGTextFillClip有数字  
    //stroke描边kCGTextFillStroke kCGTextFillStrokeClip数字黑白相映
    // kCGTextStroke kCGTextStrokeClip数字为空心的黑边
    
	CGContextSetTextMatrix (context, CGAffineTransformMake(1, 0, 0, -1, 0, 0)); 
    
    //从文本空间到用户控件的转换矩阵 删除的话数字是倒放的
    
	CGContextShowTextAtPoint(context, point.x - endpoint.x / 2.0f, 
                             point.y + stringSize.height / 4.0f, 
                             [text UTF8String], text.length); //绘制文本
    
	CGContextRestoreGState(context);
}

 

- (UIImage *) createImageWithColor: (UIColor *) color
{
    //创建一个基于位图的上下文(context),并将其设置为当前上下文(context)。 
    //参数为新创建的位图上下文的大小。
    //UIGraphicsBeginImageContext (CGSize)截图 ,是从屏幕原点开始截
    //取size大小的图片,如何截取任意 起点 开始  size  大小的图片
    
	UIGraphicsBeginImageContext(CGSizeMake(SIDE_LENGTH, SIDE_LENGTH));
	CGContextRef context = UIGraphicsGetCurrentContext();

	// 画出一个圆
	[color setFill];
	CGContextAddEllipseInRect(context, 
                              CGRectMake(0.0f, 0.0f, SIDE_LENGTH, SIDE_LENGTH));
	CGContextFillPath(context);  // 颜色填充整个圆
	
	// 画出圆圈中的数字
	[[UIColor whiteColor] setFill];
	NSString *numstring = [NSString stringWithFormat:@"%d", count++];
	centerText(context, @"Georgia", 18.0f, numstring, 
               CGPointMake(SIDE_LENGTH / 2.0f, SIDE_LENGTH / 2.0f),
               [UIColor whiteColor]);   //圆圈里的数字
	
	//描绘圆的边界 本程序用的是白线
    
	CGContextSetStrokeColorWithColor(context, [[UIColor whiteColor] CGColor]);
    
    //如果没有的话则为默认用黑色填充边界
    
	CGContextAddEllipseInRect(context, CGRectMake(INSET_AMT, INSET_AMT, 
                                                  SIDE_LENGTH - 2.0f * INSET_AMT,
                                                  SIDE_LENGTH - 2.0f * INSET_AMT));  
	CGContextStrokePath(context);

	UIImage *theImage = UIGraphicsGetImageFromCurrentImageContext(); //返回的图形大小
	UIGraphicsEndImageContext();
	return theImage;
}

 

#define RANDLEVEL ((random() % 128) / 256.0f)

- (void) add: (id) sender
{
	UIColor *color = [UIColor colorWithRed:RANDLEVEL green:RANDLEVEL 
                                      blue:RANDLEVEL alpha:1.0f];
    
	UIImage *newimage = [self createImageWithColor:color];
    
	UIImageView *newview=[[UIImageView alloc] initWithImage:newimage];
    
	newview.center = [newview randomCenterInView:[self.view viewWithTag:101]
                                       withInset:0];
    
	[[self.view viewWithTag:101] addSubview:newview];
    
	[newview release];
}

 

- (void) viewDidLoad
{
	srandom(time(0));
	count = 1;
	
	self.navigationController.navigationBar.tintColor = COOKBOOK_PURPLE_COLOR;
	self.navigationItem.rightBarButtonItem = BARBUTTON(@"Add", @selector(add:));
	
	UIView *outerView = [[UIView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 240.0f, 240.0f)];
	outerView.center = CGPointMake(160.0f, 140.0f);
	outerView.backgroundColor = [UIColor lightGrayColor];
	outerView.tag = 101;
	[self.view addSubview:outerView];
	[outerView release];
}

 

 

 

 

 

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值