美文网首页
使用UIGraphicsBeginImageContextWit

使用UIGraphicsBeginImageContextWit

作者: 嚼华先森 | 来源:发表于2017-12-08 14:46 被阅读9次

// 联系上下文

UIGraphicsBeginImageContextWithOptions(self.view.frame.size,NO,0);

NSMutableParagraphStyle*style = [NSMutableParagraphStyledefaultParagraphStyle].mutableCopy;

style.lineBreakMode=NSLineBreakByCharWrapping;

style.alignment=NSTextAlignmentLeft;

style.firstLineHeadIndent=30;

NSString*text =@"但是在iPhone的视网膜屏幕上,如你使用UIGraphicsBeginImageContext这个方法来获取图形上下文进行绘制的话就会出现你绘制出来的图片相当的模糊";

NSMutableAttributedString*attributeString = [[NSMutableAttributedStringalloc]initWithString:textattributes:@{

NSFontAttributeName:[UIFontsystemFontOfSize:15.0],NSForegroundColorAttributeName:[UIColorredColor],NSParagraphStyleAttributeName:style,

}];

[attributeStringdrawInRect:CGRectMake(20,100,self.view.frame.size.width-40, attributeString.size.height+60)];

// 根据上下文生成图片

UIImage*image =UIGraphicsGetImageFromCurrentImageContext();

UIGraphicsEndImageContext();

// 将生成的图片添加到contents

self.view.layer.contents= (id)image.CGImage;

相关文章

网友评论

      本文标题:使用UIGraphicsBeginImageContextWit

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