美文网首页
iOS绘制矩形变换

iOS绘制矩形变换

作者: Whimer | 来源:发表于2017-07-21 10:03 被阅读0次

    - (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;

    }

    相关文章

      网友评论

          本文标题:iOS绘制矩形变换

          本文链接:https://www.haomeiwen.com/subject/ywvskxtx.html