尝试将this answer转换为Swift,但我不确定如何处理此行:
[self.image drawInRect:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
感谢任何帮助,谢谢!
答案 0 :(得分:2)
你可以这样做
self.image.drawInRect(CGRect(x: 0, y: 0, width: self.frame.size.width, height: self.frame.size.height))
或
self.tempDrawImg.image .drawInRect(CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height))
答案 1 :(得分:1)
该答案中的Canvas
类是UIImageView
的子类,因此该行中的self.image
是继承的image
属性,类型为UIImage
。在Swift中,转换后的行可能类似于:
self.image.drawInRect(CGRect(x: 0, y: 0, width: self.frame.size.width, height: self.frame.size.height))
答案 2 :(得分:1)
你可以这样做
self.image.drawInRect(CGRect(x: 0, y: 0, width: self.frame.size.width, height: self.frame.size.height))
或
self.tempDrawImg.image .drawInRect(CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height))
以便完整的代码变为
UIGraphicsBeginImageContext(self.frame.size);
CGContextRef ctx = UIGraphicsGetCurrentContext();
self.tempDrawImg.image .drawInRect(CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height))
CGContextSetLineCap(ctx, kCGLineCapRound);
CGContextSetLineWidth(ctx, 5.0
CGContextSetRGBStrokeColor(ctx, 1.0, 0.0, 0.0, 1.0);
CGContextBeginPath(ctx);
CGContextMoveToPoint(ctx, self.location.x, self.location.y);
CGContextAddLineToPoint(ctx, currentLocation.x, currentLocation.y);
CGContextStrokePath(ctx);
self.image = UIGraphicsGetImageFromCurrentImageContext();