我希望创建一个带有用户脸谱图片的简单节点,其中图片有圆角(或完整的圆圈)。我按如下方式创建节点:
SKNode *friend = [[SKNode alloc] init];
SKTexture *texture = [SKTexture textureWithImage:user[@"fbProfilePicture"]];
SKSpriteNode *profilePic = [SKSpriteNode spriteNodeWithTexture:texture];
[friend addChild:profilePic];
除了使用SKCropNode(这似乎是一个糟糕的解决方法)之外,我找不到任何适当的文档来创建带圆角的图像
答案 0 :(得分:6)
通过翻译上面的答案,塞巴斯蒂安的回答,这就是 Swift 的样子。该方法接收图片的名称并返回带圆角的节点。
class func roundSquareImage(imageName: String) -> SKSpriteNode {
let originalPicture = UIImage(named: imageName)
// create the image with rounded corners
UIGraphicsBeginImageContextWithOptions(originalPicture!.size, false, 0)
let rect = CGRectMake(0, 0, originalPicture!.size.width, originalPicture!.size.height)
let rectPath : UIBezierPath = UIBezierPath(roundedRect: rect, cornerRadius: 30.0)
rectPath.addClip()
originalPicture!.drawInRect(rect)
let scaledImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext();
let texture = SKTexture(image: scaledImage)
let roundedImage = SKSpriteNode(texture: texture, size: CGSizeMake(originalPicture!.size.width, originalPicture!.size.height))
roundedImage.name = imageName
return roundedImage
}
答案 1 :(得分:5)
试试这个:
// your profile picture
UIImage *fbProfilePicture = [UIImage imageNamed:@"fbProfilePicture"];
// create the image with rounded corners
UIGraphicsBeginImageContextWithOptions(fbProfilePicture.size, NO, 0);
CGRect rect = CGRectMake(0, 0, fbProfilePicture.size.width, fbProfilePicture.size.height);
[[UIBezierPath bezierPathWithRoundedRect:rect cornerRadius:20.0] addClip];
[fbProfilePicture drawInRect:rect];
UIImage *roundedImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
// use the roundedImage as texture for your sprite
SKTexture *texture = [SKTexture textureWithImage:roundedImage];
SKSpriteNode *profilePic = [SKSpriteNode spriteNodeWithTexture:texture size:CGSizeMake(fbProfilePicture.size.width, fbProfilePicture.size.height)];
[self addChild:profilePic];
圆角部分来自this answer。
答案 2 :(得分:1)
2017年......
@FXML
private void approveSelectedRenderers() {
List<Renderer> selectedRenderers = new ArrayList<>();
selectedRenderers.addAll(renderersTable.getSelectionModel().getSelectedItems());
for (Renderer renderer : selectedRenderers) {
renderer.setApproved(true);
renderers.set(renderers.indexOf(renderer), renderer);
Thread.sleep(3000); // Simulate slowly talking to the network.
}
}
这很容易......
circleMasked的代码:
(所有处理图片的项目无论如何都需要这个。)
class YourSprite: SKSpriteNode {
func yourSetupFunction() {
texture = SKTexture( image: UIImage(named: "cat")!.circleMasked! )
答案 3 :(得分:0)
已更新cipri.l的 Swift5.2
答案class func roundSquareImage(imageName: String) -> SKSpriteNode {
let originalPicture = UIImage(named: imageName)
// create the image with rounded corners
UIGraphicsBeginImageContextWithOptions(originalPicture!.size, false, 0)
let rect = CGRect(x: 0, y: 0, width: originalPicture!.size.width, height: originalPicture!.size.height)
let rectPath : UIBezierPath = UIBezierPath(roundedRect: rect, cornerRadius: 30.0)
rectPath.addClip()
originalPicture!.draw(in: rect)
let scaledImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext();
let texture = SKTexture(image: scaledImage!)
let roundedImage = SKSpriteNode(texture: texture, size: CGSize(width: originalPicture!.size.width, height: originalPicture!.size.height))
roundedImage.name = imageName
return roundedImage
}