在我的iPad应用程序中,我想制作UIView占据屏幕重要部分的屏幕截图.不幸的是,子视图非常嵌套,因此需要很长时间才能制作屏幕截图并使页面随后变形.
有没有比"通常"更快的方式?
UIGraphicsBeginImageContext(self.bounds.size);
[self.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *resultingImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
Run Code Online (Sandbox Code Playgroud)
如果可能的话,我想避免缓存或重构我的观点.
我有一个UIImageView,里面有一个图像.我通过将UIImageView的transform属性设置为CGAffineTransformMakeRotation(angle),在显示之前旋转了图像,其中angle是以弧度表示的角度.
我希望能够创建另一个UIImage,它对应于我在视图中可以看到的旋转版本.
我几乎在那里,通过旋转图像上下文我得到一个旋转的图像:
- (UIImage *) rotatedImageFromImageView: (UIImageView *) imageView
{
UIImage *rotatedImage;
// Get image width, height of the bounding rectangle
CGRect boundingRect = [self getBoundingRectAfterRotation: imageView.bounds byAngle:angle];
// Create a graphics context the size of the bounding rectangle
UIGraphicsBeginImageContext(boundingRect.size);
CGContextRef context = UIGraphicsGetCurrentContext();
// Rotate and translate the context
CGAffineTransform ourTransform = CGAffineTransformIdentity;
ourTransform = CGAffineTransformConcat(ourTransform, CGAffineTransformMakeRotation(angle));
CGContextConcatCTM(context, ourTransform);
// Draw the image into the context
CGContextDrawImage(context, CGRectMake(0, 0, imageView.image.size.width, imageView.image.size.height), imageView.image.CGImage);
// Get an image from the …Run Code Online (Sandbox Code Playgroud) 我希望能够在调用strokeText()方法之前更改字体大小和可能的字体本身.我可以改变颜色,但我还是没有看到改变字体.
Pane canvas = new Pane();
GraphicsContext gc = canvas.getGraphicsContext2D();
gc.setStroke(Color.WHITE);
gc.strokeText("Hello", 1, 1);
Run Code Online (Sandbox Code Playgroud)
有人知道怎么做吗?
现在我的目标是在我的标签上添加抗锯齿文本.如果我的研究是正确的,SWT Labels本身不支持对文本进行抗锯齿,因此我目前的解决方法是创建图像,打开文本消除锯齿,将文本绘制到该图像,然后将该图像提供给Label .
我当前的图像绘制代码如下:
Image image = new Image(Display.getDefault(), width, height);
GC gc = new GC(image);
gc.setAntialias(SWT.ON);
gc.setTextAntialias(SWT.ON);
gc.setBackground(background);
gc.fillRectangle(0, 0, width, height);
gc.setFont(font);
gc.setForeground(foreground);
int yPos = offset.y;
for (String rawLine : lines)
{
String line = rawLine.trim();
Point lineSize = gc.textExtent(line);
int xPos = offset.x;
switch (alignment)
{
case SWT.RIGHT:
xPos += width - lineSize.x;
break;
case SWT.CENTER:
xPos += width / 2 - lineSize.x / 2;
break;
case SWT.LEFT:
default:
xPos += 0;
}
gc.drawText(line, xPos, yPos, …Run Code Online (Sandbox Code Playgroud) 我正在使用这段非常优雅的代码 获取UIScrollView的屏幕截图,包括屏幕外部分,以捕获要导出到UIImage的UITableView的全部内容.
UIGraphicsBeginImageContextWithOptions(self.controller.tableView.contentSize, YES, 0);
{
CGPoint savedContentOffset = self.controller.tableView.contentOffset;
CGRect savedFrame = self.controller.tableView.frame;
self.controller.tableView.contentOffset = CGPointZero;
self.controller.tableView.frame = CGRectMake(0, 0, self.controller.tableView.contentSize.width, self.controller.tableView.contentSize.height);
[self.controller.tableView.layer renderInContext: UIGraphicsGetCurrentContext()];
image = UIGraphicsGetImageFromCurrentImageContext();
self.controller.tableView.contentOffset = savedContentOffset;
self.controller.tableView.frame = savedFrame;
}
UIGraphicsEndImageContext();
Run Code Online (Sandbox Code Playgroud)
它在模拟器中运行得非常好,但是,当我在iOS设备(iphone 4s)上运行此代码时,在调用此方法时,我会得到以下错误的全部飞跃:
[self.controller.tableView.layer renderInContext: UIGraphicsGetCurrentContext()];
Run Code Online (Sandbox Code Playgroud)
错误:
Error: CGContextTranslateCTM: invalid context 0x2924b0
Error: CGContextDrawImage: invalid context 0x2924b0
Error: CGContextRestoreGState: invalid context 0x2924b0
Error: CGContextSaveGState: invalid context 0x2924b0
Error: CGContextScaleCTM: invalid context 0x2924b0
Error: CGContextClipToRect: invalid context 0x2924b0
etc...
Run Code Online (Sandbox Code Playgroud)
当表视图内容大小在其范围内时,此代码很有效,但只要它大于其边界(即使是1个像素),它就会进入上述错误的无限循环.但这只发生在设备上.不是模拟器.
有任何想法吗?
我正在使用 JavaFx 制作一个轻量级绘画应用程序。我的LayerController类及其方法addLayer等一直存在一些问题。所以我认为编写一些 JUunit 测试用例是检查我的方法的正确性的好主意。简而言之,我正在一个自制的类中Canvas使用它GraphicsContext,我称之为PaintGraphics。这个班级负责所有的绘画工作。需要LayerControlleraPaintGraphics在各层上完成其工作。GraphicsContext但当我在测试用例中启动时,似乎出了问题。我收到错误“内部图形尚未初始化。”。我猜这与有关,GraphicsContext但我不确定。任何有关错误发生原因以及解决方法的想法将不胜感激!
测试的源代码如下所示:
package view;
import static org.junit.Assert.*;
import java.util.ArrayList;
import org.junit.Test;
import controller.LayerController;
import javafx.scene.canvas.Canvas;
import javafx.scene.layout.AnchorPane;
import model.Layer;
import model.PaintGraphics;
public class LayoutControllerTest {
Layer layer = new Layer(0, new Canvas(100,100));
ArrayList<Layer> layers = new ArrayList<Layer>();
PaintGraphics pGraphics = new PaintGraphics(layer.getCanvas().getGraphicsContext2D());
LayerController layerController;
@Test
public void addLayerTest() {
layers.add(layer);
layerController.addLayer(layer, (AnchorPane)layer.getCanvas().getParent());
}
}
Run Code Online (Sandbox Code Playgroud) canvas ×2
ios ×2
java ×2
javafx ×2
antialiasing ×1
fonts ×1
iphone ×1
junit ×1
pane ×1
performance ×1
quartz-core ×1
rotation ×1
screenshot ×1
swt ×1
text ×1
uiimage ×1
uiimageview ×1
uiscrollview ×1