我的问题是,如果我有狮子图像,我只想改变狮子的颜色而不是背景颜色.为此,我提到了这个问题,但它改变了整个图像的颜色.此外,图像看起来不太好.我需要像photoshop那样的颜色变化.是否可以在coregraphics中执行此操作,或者我必须使用任何其他库.
编辑:我需要颜色变化像iQuikColor应用程序
我有子类UIView
并添加了一个drawRect
方法.然后我使用此自定义类定义视图并向其添加子视图.
问题是drawRect
似乎在子视图下绘制东西(因此不可见).
我希望drawRect
绘制的内容出现在我的自定义子视图上方UIView
.
这可能吗?
我试着用一个简单的蓝色圆圈制作一个20x20的UIImage.我尝试使用此功能,但结果是黑色方块中的蓝色圆圈.如何移除圆圈周围的黑色方块?
功能:
+ (UIImage *)blueCircle {
static UIImage *blueCircle = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
UIGraphicsBeginImageContextWithOptions(CGSizeMake(20.f, 20.f), YES, 0.0f);
CGContextRef ctx = UIGraphicsGetCurrentContext();
CGContextSaveGState(ctx);
CGRect rect = CGRectMake(0, 0, 20, 20);
CGContextSetFillColorWithColor(ctx, [UIColor cyanColor].CGColor);
CGContextFillEllipseInRect(ctx, rect);
CGContextRestoreGState(ctx);
blueCircle = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
});
return blueCircle;
}
Run Code Online (Sandbox Code Playgroud)
实际结果:
我需要在swift中创建一个只有两个圆角的矩形(Objective C代码也可以).
目前我的代码正在创建两个矩形
CGPathCreateWithRoundedRect(CGRectMake(0, 0, 30, 60), 5, 5, nil);
Run Code Online (Sandbox Code Playgroud)
和
CGPathCreateWithRoundedRect(CGRectMake(0, 0, 30, 60), 0, 0, nil);
Run Code Online (Sandbox Code Playgroud)
合并它们(有两个直角和两个圆角)但我对代码不满意,我很确定应该有更好的方法来做到这一点.
我是iOS和图形开发的新手.
我正在使用此代码调整iPhone上的图像大小:
CGRect screenRect = CGRectMake(0, 0, 320.0, 480.0);
UIGraphicsBeginImageContext(screenRect.size);
[value drawInRect:screenRect blendMode:kCGBlendModePlusDarker alpha:1];
UIImage *tmpValue = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
Run Code Online (Sandbox Code Playgroud)
只要图像的宽高比与新调整大小的图像的宽高比相匹配,哪个工作正常.我想修改它,以便保持正确的宽高比,并在图像不显示的任何地方放置黑色背景.所以我仍然会得到一张320x480的图像,但在顶部和底部或两侧都有黑色,具体取决于原始图像尺寸.
有没有一种简单的方法来做到这一点类似于我正在做的事情?谢谢!
我有非常大的图形Mac应用程序,现在我在10.9 GM的控制台上收到了很多以下消息.
<Error>: Error: this application, or a library it uses, has passed an invalid numeric value (NaN, or not-a-number) to CoreGraphics API. This is a serious error and contributes to an overall degradation of system stability and reliability. This notice is a courtesy: please fix this problem. It will become a fatal error in an upcoming update.
Run Code Online (Sandbox Code Playgroud)
我注意到这些消息在调用[NSApp nextEventMatchingMask:untilDate inMode:dequeue]之后出现在调试器中,但我认为原因在于其他一些地方.但我有太多地方使用Cocoa Graphics.我在10.9之前没有收到这种消息.
如何检测NaN传递给CoreGraphics API的位置?
我需要在Draw应用程序的末尾用箭头绘制线条.我不擅长三角学,所以无法解决这个问题.
用户将手指放在屏幕上并沿任何方向画线.因此,箭头应出现在行尾.
我目前正在尝试获取UIImageView中像素的alpha值.我从[UIImageView image]中获取了CGImage,并从中创建了一个RGBA字节数组.Alpha是预乘的.
CGImageRef image = uiImage.CGImage;
NSUInteger width = CGImageGetWidth(image);
NSUInteger height = CGImageGetHeight(image);
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
rawData = malloc(height * width * 4);
bytesPerPixel = 4;
bytesPerRow = bytesPerPixel * width;
NSUInteger bitsPerComponent = 8;
CGContextRef context = CGBitmapContextCreate(
rawData, width, height, bitsPerComponent, bytesPerRow, colorSpace,
kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big
);
CGColorSpaceRelease(colorSpace);
CGContextDrawImage(context, CGRectMake(0, 0, width, height), image);
CGContextRelease(context);
Run Code Online (Sandbox Code Playgroud)
然后,我使用UIImageView中的坐标计算给定alpha通道的数组索引.
int byteIndex = (bytesPerRow * uiViewPoint.y) + uiViewPoint.x * bytesPerPixel;
unsigned char alpha = rawData[byteIndex + 3];
Run Code Online (Sandbox Code Playgroud)
但是我没有得到我期望的价值.对于图像的完全黑色透明区域,我得到alpha通道的非零值.我是否需要翻译UIKit和Core Graphics之间的坐标 - …
鉴于iOS上的任意UIView,是否有一种方法可以使用Core Graphics(CAGradientLayer)来应用"前景透明"渐变?
我不能使用标准的CAGradientLayer,因为背景比UIColor更复杂.我也无法覆盖PNG,因为背景会随着我的子视图沿其父垂直滚动视图滚动而改变(见图).
我有一个非优雅的后备:让我的uiview剪辑其子视图,并在滚动父滚动视图时移动背景的预渲染渐变png.
我有一个CAKeyframeAnimation
动画,我想永远重复使用repeatCount = HUGE_VALF
.动画的持续时间是2秒,但我想在每个周期之前暂停3秒.
我能想到的唯一两种方法是:
使整个动画持续5秒,并添加额外的keyTimes和值,以便在5s动画的最后3s中获得我正在寻找的暂停.这感觉有点哈哈.
让动画只重复一次然后添加使用类似于performSelector:afterDelay:2
再次运行动画,等等.这也很脏.也意味着我需要addAnimation:
每5秒调用一次,我不确定它在性能方面是否是最佳的.
还有其他选择我可能会失踪吗?这两种方法中哪一种比另一种更好?