iOS绘制矩形变换

- (void)viewDidLoad

{

[super viewDidLoad];

self.view.backgroundColor = [UIColor whiteColor];

UIImageView* iv = [[UIImageView alloc]

initWithImage: [self drawImage:self.view.frame.size]];

[self.view addSubview:iv];

//旋转

CGAffineTransform CGAffineTransformMakeRotation ( CGFloat angle );

[UIView animateWithDuration:2.0

animations:^{

iv.transform = CGAffineTransformRotate(CGAffineTransformScale(iv.transform, 0.2, 0.2), 1*M_PI); //缩放+旋转

}];

CGAffineTransform CGAffineTransformMakeScale ( CGFloat sx, CGFloat sy );

//缩放

[UIView animateWithDuration:2.0

animations:^{

iv.transform=CGAffineTransformScale(iv.transform, 2.0, 4.0); //实现的是放大和缩小

}];

//平移

CGAffineTransform CGAffineTransformMakeTranslation ( CGFloat tx, CGFloat ty ); //平移

[UIView animateWithDuration:2.0

animations:^{

iv.transform=CGAffineTransformTranslate(iv.transform, 3, 3); //平移

}];

}

- (UIImage*) drawImage:(CGSize) size

{

// 创建内存中的图片

UIGraphicsBeginImageContext(size);

CGContextRef ctx = UIGraphicsGetCurrentContext();

// 设置线宽

CGContextSetLineWidth(ctx, 8);

// ---------下面开始向内存中绘制图形---------

// 设置线条颜色

CGContextSetRGBStrokeColor(ctx, 0 , 1, 0 , 1);

// 绘制一个矩形边框

//CGContextStrokeRect(ctx , CGRectMake(30 , 30 , 120 , 60));

// 设置填充颜色

CGContextSetRGBFillColor(ctx, 1, 1, 0 , 1);

// 绘制一个矩形边框

CGContextFillRect(ctx , CGRectMake(20 , 300, 40 , 60));

// 设置线条颜色

CGContextSetRGBStrokeColor(ctx, 0, 1 , 1 , 1);

// 获取该绘图Context中的图片

UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();

// ---------结束绘图---------

UIGraphicsEndImageContext();

// 获取当前应用路径下的Documents目录下的指定文件名对应的文件路径

NSString *path = [[NSHomeDirectory()

stringByAppendingPathComponent:@"Documents"]

stringByAppendingPathComponent:@"newPng.png"];

// 保存PNG图片

[UIImagePNGRepresentation(newImage)

writeToFile:path atomically:YES];

return newImage;

}

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容