日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Quartz2D知识点聚合案例

發布時間:2023/11/30 编程问答 33 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Quartz2D知识点聚合案例 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

Quartz2D知識點聚合

基本

//畫圖片UIImage *image = [UIImage imageNamed:@"阿貍頭像"];[image drawInRect:rect];//字體NSString *title = @"標題";NSMutableDictionary *atr = [NSMutableDictionary dictionary];atr[NSFontAttributeName] = [UIFont systemFontOfSize:15];// atr[NSForeground?ColorAttributeName] = [UIColor greenColor];[title drawInRect:CGRectMake(120, 20, 100, 20) withAttributes:atr];//橢圓UIBezierPath *path = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(10, 20, 70, 130)];[path stroke];//方形UIBezierPath *path1 = [UIBezierPath bezierPathWithRect:CGRectMake(10, 200, 10, 50)];[path1 stroke];//圓角方形UIBezierPath *path2 = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(100, 30, 100, 100) cornerRadius:10];[path2 stroke];//一個角圓角UIBezierPath *path3 = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(210, 90, 80, 70) byRoundingCorners:UIRectCornerTopRight cornerRadii:CGSizeMake(20, 30)];[path3 stroke];//圓弧UIBezierPath *path4 = [UIBezierPath bezierPathWithArcCenter:CGPointMake(130, 230) radius:70 startAngle:0 endAngle:M_PI clockwise:YES];[path4 stroke]; // 1.獲得當前上下文CGContextRef ctx = UIGraphicsGetCurrentContext();// 2.拼接路徑UIBezierPath *path = [UIBezierPath bezierPath];[path moveToPoint:CGPointMake(10, 20)];[path addQuadCurveToPoint:CGPointMake(200, 80) controlPoint:CGPointMake(100, 200)];// 3. 添加路徑到上下文CGContextAddPath(ctx, path.CGPath);// 4.渲染上下文CGContextStrokePath(ctx);

變換

//變換//平移 // CGContextTranslateCTM(ctx, 10, 20);//旋轉CGContextRotateCTM(ctx, M_PI_4);//縮放CGContextScaleCTM(ctx, 1.2, 1.2);

上下文棧

  • 先保存或者還原上下文棧,再設置狀態
// 1.獲得當前上下文CGContextRef ctx = UIGraphicsGetCurrentContext();// 2.拼接路徑UIBezierPath *path = [UIBezierPath bezierPath];[path moveToPoint:CGPointMake(10, 20)];[path addQuadCurveToPoint:CGPointMake(200, 80) controlPoint:CGPointMake(100, 200)];// 3. 添加路徑到上下文CGContextAddPath(ctx, path.CGPath);//保存上下文CGContextSaveGState(ctx);//設置上下文狀態CGContextSetLineWidth(ctx, 10);[[UIColor redColor] set];// 4.渲染上下文CGContextStrokePath(ctx);// 2.拼接路徑path = [UIBezierPath bezierPath];[path moveToPoint:CGPointMake(100, 80)];[path addLineToPoint:CGPointMake(200, 200)];// 3. 添加路徑到上下文CGContextAddPath(ctx, path.CGPath);//還原上下文CGContextRestoreGState(ctx);//設置上下文狀態CGContextSetLineWidth(ctx, 5);[[UIColor blueColor] set];// 4.渲染上下文CGContextStrokePath(ctx);

生成圖片

UIImage *image = [UIImage imageNamed:@"小黃人"];UIGraphicsBeginImageContextWithOptions(image.size, YES, 0);[image drawAtPoint:CGPointZero];NSString *str = @"小黃人";[str drawAtPoint:CGPointZero withAttributes:nil];image = UIGraphicsGetImageFromCurrentImageContext();UIGraphicsEndImageContext();

截圖

  • 給定裁減區域再渲染
//開啟圖片上下文UIGraphicsBeginImageContext(view.frame.size);//獲得當前上下文CGContextRef ctx = UIGraphicsGetCurrentContext();//給定裁減區域-----//渲染圖片[view.layer renderInContext:ctx];//從當前上下文得到一張圖片UIImage *image = UIGraphicsGetImageFromCurrentImageContext();//關閉圖片上下文UIGraphicsEndImageContext();return image;

擦除

  • 先渲染到上下文,再擦除
UITouch *touch = [touches anyObject];CGPoint point = [touch locationInView:self.imageView];//開啟上下文UIGraphicsBeginImageContextWithOptions(self.imageView.frame.size, NO, 0);//獲得當前上下文CGContextRef ctx = UIGraphicsGetCurrentContext();//渲染到上下文[self.imageView.layer renderInContext:ctx];//獲取擦除區域CGRect rect = CGRectMake(point.x - 10, point.y - 10, 20, 20);// 擦除上下文的內容CGContextClearRect(ctx, rect);// 生成圖片UIImage *image = UIGraphicsGetImageFromCurrentImageContext();_imageView.image = image;// 關閉上下文UIGraphicsEndImageContext();

轉載于:https://www.cnblogs.com/ShaoYinling/p/4649718.html

總結

以上是生活随笔為你收集整理的Quartz2D知识点聚合案例的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。