- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我真的需要使用 UIImage 模拟 NSImage 的 drawInRect:fromRect:operation:fraction 的行为,我想知道什么是最好的方法或(更好),如果有人已经这样做并愿意分享代码。
我尝试使用 CGImageCreateWithImageInRect 执行此操作(请参阅最后的代码),但我没有得到预期的结果。请注意,代码实际上是在 mac 上测试的(我现在无法在 iPhone 上运行整个代码),所以我在从 NSImage 获取 CGImage 时可能会遇到一些问题
- (void)drawInRect:(CGRect)rect fromRect:(CGRect)fromRect alpha:(float)alpha {
CGContextRef context;
CGImageRef originalImageRef;
CGImageRef subimageRef;
#if ERMac
context=[[NSGraphicsContext currentContext] graphicsPort];
CGImageSourceRef source;
source = CGImageSourceCreateWithData((CFDataRef)[self TIFFRepresentation], NULL);
originalImageRef = CGImageSourceCreateImageAtIndex(source, 0, NULL);
CFRelease(source);
#else
context=UIGraphicsGetCurrentContext();
originalImageRef=CGImageRetain([self CGImage]);
#endif
subimageRef = CGImageCreateWithImageInRect (originalImageRef, fromRect);
CGContextDrawImage(context, rect, subimageRef);
if (subimageRef)
CGImageRelease(subimageRef);
if (originalImageRef)
CGImageRelease(originalImageRef);
}
最佳答案
我就是这样做的...
- (void)drawInRect:(CGRect)drawRect fromRect:(CGRect)fromRect operation:(CGBlendMode)blendMode fraction:(CGFloat)alpha
{
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSaveGState(context);
CGContextClipToRect(context, drawRect);
CGFloat xScale = (drawRect.size.width/fromRect.size.width);
CGFloat yScale = (drawRect.size.height/fromRect.size.height);
CGFloat scale = 1;
if([self respondsToSelector:@selector(scale)])
{
scale = self.scale;
}
CGFloat actualWidth = (xScale * self.size.width)*scale;
CGFloat actualHeight = (yScale * self.size.height)*scale;
CGFloat xInset = fromRect.origin.x * xScale;
CGFloat yInset = fromRect.origin.y * yScale;
// Take care of Y-axis inversion problem by translating the context on the y axis
CGContextTranslateCTM(context, 0, drawRect.size.height);
CGContextScaleCTM(context, 1.0, -1.0);
CGContextDrawImage(context, CGRectMake(-xInset + drawRect.origin.x, -yInset + drawRect.origin.y, actualWidth, actualHeight), self.CGImage);
CGContextRestoreGState(context);
}
关于iphone - 模拟NSImage绘制矩形:fromRect:operation:fraction with UIImage,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2306644/
我有一个矩形 NSImage A,我想缩放以嵌入到方形透明图像 B 中,保持 A 的比例。所以,最后我会得到一个带有矩形的方形图像。 我该如何构图?我的意思是,如何在另一个 NSImage 上绘制一个
我有一堆 NSImages,我将它们保存在 PDFDocument 中,作为 PDFPages。当我第一次插入 NSImage 时,NSImage 的大小以点 (72 dpi) 为单位,而(仅)NSI
我试图理解为什么 var tmp: Any = NSImage(byReferencingFile: somePath) var img: NSImage = tmp as NSImage 不会工作并
好吧,我很困惑,因为这应该很简单。 在 Xcode 5 中,我开始了一个新的 iphone 项目。在 ViewDidLoad 中,我开始输入: NSImage *图像 ... 实际上在“NSI”点……
我正在尝试将文件从 xcassets 加载到 NSImage,其中 Assets 名称是 Logo 。 我试过这个: let logoIcon = NSImage(named: "logo") 但我一
我正在编写一个简单的 MacRuby 应用程序。我有一个 NSView,它在调用 drawRect() 时在自身内部绘制图像。 在那个 NSView 子类中,我的 drawRect() 有(以及其他一
所以我有一个 NSImage *startingImage 它由带有灰色色彩空间的 NSBitmapImageRep 表示 我需要反转它的颜色,因此我将其转换为 CIImage CIImage *
首先,我知道我不应该使用 SQLite 数据库来存储图像,但我只存储非常小的网站图标。 我的问题是我尝试将这些图标插入数据库(似乎有效),我将图标转换为 NSData与 -tiffrepresenta
我正在尝试制作一个简单的基于文档的 cocoa 应用程序,可以在 pdf 文件中保存和加载图像。 对于保存部分,我正在使用 - (NSData *)dataOfType:(NSString *)typ
我使用 NSAffineTransform 类在其中心旋转了 NSImage,它可以工作。代码在这里: - (NSImage*)rotateImage: (NSImage*)myImage angle
我是 cocoa 和编程的新手,如果这是基本的东西,我很抱歉。 我正在使用 ARC。我有一个 NSImageView 组件,它由 DropZone 类控制。我将图像拖放到其中,但是一旦我尝试调用我得到
我想创建 NSScrollView 对象的 NSImage,这样我就可以将平面图形用于动画目的。 当我将 ScrollView 对象渲染为图形并将其添加回我的窗口时,它可以工作,但看起来非常糟糕,就像
到目前为止,我已经构建了一个应用程序,允许用户将图像拖放到 NSImageView 上。但是,我希望能够通过简单地单击任何图像并按住鼠标按钮来移动其位置来移动这些图像。 设置图像后如何操作 NSIma
我正在尝试打印 NSImage,但无法将其调整为正确的尺寸。我希望 NSImage 在任何足够大的纸张上打印为 4"x6"照片。这是我现在的代码。 var printView = NSImageVie
这可能是一个新问题,但我无法理解如何操作 NSImage 实例。 我正在尝试创建一个方法,该方法将采用 NSImage (一个图标),在其上绘制另一个 NSImage (复选标记),然后返回修改后的
由于 Cocoa-Java 已弃用,我正在将 Cocoa-Java 代码迁移到 Cocoa + JNI。该代码打印存储在文件中的图像。新的 Cocoa 代码基本上是: NSImage *image =
我正在尝试设置一个自定义拖动图标以在 NSTableView 中使用。一切似乎都正常,但由于我对 Quartz 缺乏经验,我遇到了问题。 - (NSImage *)dragImageForRowsWi
我有许多 NSImage,正在自定义 NSView 子类中渲染。当用户将鼠标放在这些图像上时,我希望放大这些图像,并且想知道执行此操作的最佳方法。 目前我正在使用 NSView 的 drawRect:
我的程序中有一个大型一维动态数组,它表示磁盘上的 FITS 图像,即它保存图像的所有像素值。数组的类型是double。目前,我只关心单色图像。 由于 Cocoa 不直接支持 FITS 格式,因此我使用
我想获取 NSImage.h 中定义的常量的图像文件,例如 NSImageNameGoRightTemplate 。我想复制并编辑其中一些。有谁知道这些图像位于哪里?我太笨了,在驱动器上找不到它们..
我是一名优秀的程序员,十分优秀!