- (void)viewDidLoad
{
[super viewDidLoad];
self.view.backgroundColor = [UIColor whiteColor];
UIImageView* iv = [[UIImageView alloc]
initWithImage: [self drawImage:self.view.frame.size]];
[self.view addSubview:iv];
//旋轉(zhuǎn)
CGAffineTransform CGAffineTransformMakeRotation ( CGFloat angle );
[UIView animateWithDuration:2.0
animations:^{
iv.transform = CGAffineTransformRotate(CGAffineTransformScale(iv.transform, 0.2, 0.2), 1*M_PI); //縮放+旋轉(zhuǎn)
}];
CGAffineTransform CGAffineTransformMakeScale ( CGFloat sx, CGFloat sy );
//縮放
[UIView animateWithDuration:2.0
animations:^{
iv.transform=CGAffineTransformScale(iv.transform, 2.0, 4.0); //實(shí)現(xiàn)的是放大和縮小
}];
//平移
CGAffineTransform CGAffineTransformMakeTranslation ( CGFloat tx, CGFloat ty ); //平移
[UIView animateWithDuration:2.0
animations:^{
iv.transform=CGAffineTransformTranslate(iv.transform, 3, 3); //平移
}];
}
- (UIImage*) drawImage:(CGSize) size
{
// 創(chuàng)建內(nèi)存中的圖片
UIGraphicsBeginImageContext(size);
CGContextRef ctx = UIGraphicsGetCurrentContext();
// 設(shè)置線寬
CGContextSetLineWidth(ctx, 8);
// ---------下面開始向內(nèi)存中繪制圖形---------
// 設(shè)置線條顏色
CGContextSetRGBStrokeColor(ctx, 0 , 1, 0 , 1);
// 繪制一個(gè)矩形邊框
//CGContextStrokeRect(ctx , CGRectMake(30 , 30 , 120 , 60));
// 設(shè)置填充顏色
CGContextSetRGBFillColor(ctx, 1, 1, 0 , 1);
// 繪制一個(gè)矩形邊框
CGContextFillRect(ctx , CGRectMake(20 , 300, 40 , 60));
// 設(shè)置線條顏色
CGContextSetRGBStrokeColor(ctx, 0, 1 , 1 , 1);
// 獲取該繪圖Context中的圖片
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
// ---------結(jié)束繪圖---------
UIGraphicsEndImageContext();
// 獲取當(dāng)前應(yīng)用路徑下的Documents目錄下的指定文件名對應(yīng)的文件路徑
NSString *path = [[NSHomeDirectory()
stringByAppendingPathComponent:@"Documents"]
stringByAppendingPathComponent:@"newPng.png"];
// 保存PNG圖片
[UIImagePNGRepresentation(newImage)
writeToFile:path atomically:YES];
return newImage;
}