相关疑难解决方法(0)

如何在iPhone中为透明的PNG图像着色?

我知道可以通过在其上绘制CGContextFillRect并设置混合模式来着色矩形图像.但是,我无法弄清楚如何对透明图像(如图标)进行色调处理.它必须是可能的,因为SDK本身就是在tab-bar上自己做的.有人能提供一个片段吗?

更新:

自从我最初提出这个问题以来,已经给出了很多很好的建议.请务必仔细阅读所有答案,找出最适合您的方法.

更新(2015年4月30日):

使用iOS 7.0,我现在可以执行以下操作,这将满足我原始问题的需要.但如果您有更复杂的案例,请查看所有答案.

UIImage *iconImage = [[UIImage imageNamed:@"myImageName"] imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];    
UIImageView *icon = [[UIImageView alloc] initWithImage:iconImage];
icon.tintColor = [UIColor redColor];
Run Code Online (Sandbox Code Playgroud)

iphone image tint uiimage ios

73
推荐指数
6
解决办法
4万
查看次数

在CALayer中添加UIImage

我必须添加一个UIImageView作为MapView的子视图.为此,我在MapView上创建了一个图层.在这一层,我想放置我的图像,但我得到一个白色的矩形,没有别的.我的图片不可见.

这是代码:

- (void)viewDidLoad
{
    //......

    CALayer *layer = [CALayer layer];
    layer.backgroundColor = [[UIColor whiteColor] CGColor];

    if(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
    {
        layer.bounds = CGRectMake(self.mapView.bounds.origin.x,
                                  self.mapView.bounds.origin.y, 80, 300);
    }
    else
    {
        layer.bounds = CGRectMake(self.mapView.frame.origin.x,
                                  self.mapView.frame.origin.y, 150, 700);
    }

    layer.contents = (id)[UIImage imageNamed:@"myImage.png"];
    //the name is correct but  in the output the image is not visible

    [[self.mapView layer] addSublayer:layer];
    [layer setNeedsDisplay];
}
Run Code Online (Sandbox Code Playgroud)

subview calayer uiimageview uiimage ios

40
推荐指数
3
解决办法
4万
查看次数

调整从相机中提取的UIimages的大小也会旋转UIimage?

我从相机获取UIimages并将它们分配给UIImageViews进行显示.当我这样做时,相机给我一个1200 x 1600像素的图像,然后我分配给我的应用程序中的UIImageView.在此条件下,图像在图像视图中按预期显示.但是,当我在将其分配给UIImageView之前尝试调整检索到的UIImage时,图像正在按预期调整大小但是在某处(在RESIZING代码中?)我的UIImage正在进行ROTATED ...结果,当我将调整大小的UIImage分配给UIImageView时,图像旋转90度并在宽高比(1200 x 1600像素)不变的情况下显示为拉伸...

我正在使用它来从相机获取UIImage:

- (void) imagePickerController:(UIImagePickerController*)picker didFinishPickingMediaWithInfo:(NSDictionary*)info
{

        myImg = [info objectForKey:@"UIImagePickerControllerOriginalImage"];
        myResizedImg = [self resizeImage:myImg width:400 height:533];
        [myImageView setImage:myResizedImg];

}
Run Code Online (Sandbox Code Playgroud)

我用它来调整它的大小:

-(UIImage *)resizeImage:(UIImage *)anImage width:(int)width height:(int)height
{

    CGImageRef imageRef = [anImage CGImage];

    CGImageAlphaInfo alphaInfo = CGImageGetAlphaInfo(imageRef);

    if (alphaInfo == kCGImageAlphaNone)
    alphaInfo = kCGImageAlphaNoneSkipLast;


    CGContextRef bitmap = CGBitmapContextCreate(NULL, width, height, CGImageGetBitsPerComponent(imageRef), 4 * width, CGImageGetColorSpace(imageRef), alphaInfo);

    CGContextDrawImage(bitmap, CGRectMake(0, 0, width, height), imageRef);

    CGImageRef ref = CGBitmapContextCreateImage(bitmap);
    UIImage *result = [UIImage imageWithCGImage:ref];

    CGContextRelease(bitmap);
    CGImageRelease(ref);

    return result;  
} …
Run Code Online (Sandbox Code Playgroud)

iphone uiimageview uiimagepickercontroller uiimage

35
推荐指数
2
解决办法
3万
查看次数

核心图形CGImage掩码不会影响

我实际上要做的是在视图中有一个文本标签"切割"一个文本形状的孔.我试过使用self.mask = uiLabel但是那些拒绝正确定位文本所以我通过Core Graphics接近这个.

这是不起作用的代码(在draw(_ rect: CGRect))中:

    let context = (UIGraphicsGetCurrentContext())!

    // Set mask background color
    context.setFillColor(UIColor.black.cgColor)
    context.fill(rect)

    context.saveGState()


    let paragraphStyle = NSMutableParagraphStyle()
    paragraphStyle.alignment = .center

    let attributes = [
        NSParagraphStyleAttributeName: paragraphStyle,
        NSFontAttributeName: UIFont.systemFont(ofSize: 16, weight: UIFontWeightMedium),
        NSForegroundColorAttributeName: UIColor.white
    ]

    let string = NSString(string: "LOGIN")

    // This wouldn't vertically align so we calculate the string size and create a new rect in which it is vertically aligned
    let size = string.size(attributes: attributes)
    let position = CGRect( …
Run Code Online (Sandbox Code Playgroud)

core-graphics cgimage cgimagemaskcreate ios swift

15
推荐指数
1
解决办法
550
查看次数

swift 2.0之后的CGBitmapInfo alpha掩码

我正在使用来自此github repo https://github.com/Haneke/HanekeSwift的库来缓存从服务器下载的图像.更新到swift 2.0后,一切都搞砸了.

我已经能够解决所有问题,但这个功能:

func hnk_decompressedImage() -> UIImage! {
    let originalImageRef = self.CGImage
    let originalBitmapInfo = CGImageGetBitmapInfo(originalImageRef)
    let alphaInfo = CGImageGetAlphaInfo(originalImageRef)

    // See: http://stackoverflow.com/questions/23723564/which-cgimagealphainfo-should-we-use
    var bitmapInfo = originalBitmapInfo
    switch (alphaInfo) {
    case .None:
        bitmapInfo &= ~CGBitmapInfo.AlphaInfoMask
        bitmapInfo |= CGBitmapInfo(rawValue: CGImageAlphaInfo.NoneSkipFirst.rawValue)
    case .PremultipliedFirst, .PremultipliedLast, .NoneSkipFirst, .NoneSkipLast:
        break
    case .Only, .Last, .First: // Unsupported
        return self
    }

    let colorSpace = CGColorSpaceCreateDeviceRGB()
    let pixelSize = CGSizeMake(self.size.width * self.scale, self.size.height * self.scale)
    if let context = CGBitmapContextCreate(nil, Int(ceil(pixelSize.width)), Int(ceil(pixelSize.height)), CGImageGetBitsPerComponent(originalImageRef), 0, colorSpace, …
Run Code Online (Sandbox Code Playgroud)

alpha-transparency ios swift swift2

14
推荐指数
1
解决办法
5647
查看次数

使用CoreGraphics CGContextDrawImage在CATiledLayer中绘图

我想在iPhone OS 3.1.3中使用CATiledLayer并且这样做所有绘图-(void)drawLayer:(CALayer *)layer inContext:(CGContext)context只需要使用coregraphics.

现在我遇到了iPhone上翻转坐标系的问题,并且有一些建议如何使用变换来修复它:

我的问题是我无法让它发挥作用.我开始使用PhotoScroller示例代码并仅使用coregraphics调用替换绘图方法.看起来像这样

- (void)drawLayer:(CALayer *)layer inContext:(CGContextRef)context {
CGContextSaveGState(context);
CGRect rect = CGContextGetClipBoundingBox(context);
CGFloat scale = CGContextGetCTM(context).a;
CGContextConcatCTM(context, CGAffineTransformMakeTranslation(0.f, rect.size.height));
CGContextConcatCTM(context, CGAffineTransformMakeScale(1.f, -1.f));

CATiledLayer *tiledLayer = (CATiledLayer *)layer;
CGSize tileSize = tiledLayer.tileSize;


tileSize.width /= scale;
tileSize.height /= scale;
// calculate the rows and columns of tiles that intersect the rect we have been asked to draw
int firstCol = floorf(CGRectGetMinX(rect) / tileSize.width);
int lastCol = floorf((CGRectGetMaxX(rect)-1) / tileSize.width);
int firstRow = floorf(CGRectGetMinY(rect) …
Run Code Online (Sandbox Code Playgroud)

iphone core-graphics cgcontext catiledlayer

6
推荐指数
1
解决办法
8408
查看次数

使用CGContext时的图像绘制问题.图像水平镜像

当我UIImage* image使用下面的代码绘制UIView时,图像是水平镜像的.如果我画4,它就是这样的替代文字 ..

CGRect rect = CGRectMake(x, y, imageWidth, imageHeight);
CGContextDrawImage((CGContextRef) g, rect, ((UIImage*)image).CGImage);
Run Code Online (Sandbox Code Playgroud)

那就是问题所在???我做错了??? 或者如果有人知道如何解决它,请让我也知道.我非常感谢提前.

谢谢你的loooooooooot.

iphone drawing uiimage cgcontext

2
推荐指数
1
解决办法
7195
查看次数

iPhone MapKit:自定义MKPolygonView,图像颠倒

有谁知道如何解决这个问题:我自定义MKPolygonView中的图像是颠倒翻转的吗?

这个想法(这已经工作正常)有一个"SnowmapsOverlayView"类,它扩展了"MKPolygonView",它显示了一个图像.此图片在地图上具有默认位置和大小(充当Google Maps Web API中的GroundOverlay).这一切都正常,但图像颠倒显示.我尝试了以下选项,但没有结果:

传递UIImage.CGImage时,CGContextDrawImage将图像上下颠倒

感谢您的任何帮助或建议!

我的代码:

#import <MapKit/MapKit.h>

@interface SnowmapsOverlayView : MKPolygonView
{
}

@end

-----------------        

#import "SnowmapsOverlayView.h"

@implementation SnowmapsOverlayView

-(id)initWithPolygon:(MKPolygon *)polygon
{
    self = [super initWithPolygon:polygon];

    return self;    
}

-(void)drawMapRect:(MKMapRect)mapRect zoomScale:(MKZoomScale)zoomScale inContext:(CGContextRef)context
{
    UIImage *image = [UIImage imageNamed:@"snowmap.png"];

    //This should do the trick, but is doesn't.. maybe a problem with the "context"?
    //CGRect imageRect = CGRectMake(0, 0, image.size.width, image.size.height);
    //CGContextTranslateCTM(context, 0, image.size.height);
    //CGContextScaleCTM(context, 1.0, -1.0);

    CGRect overallCGRect = [self rectForMapRect:self.overlay.boundingMapRect];
    CGContextDrawImage(context, overallCGRect, image.CGImage);
}

-(BOOL)canDrawMapRect:(MKMapRect)mapRect zoomScale:(MKZoomScale)zoomScale
{ …
Run Code Online (Sandbox Code Playgroud)

iphone image cgcontext mapkit

1
推荐指数
1
解决办法
3846
查看次数

在UIImage到纹理转换中,纹理垂直翻转

我试图在iOS平台上将UIImage读入纹理.我发现StackOverflow上的代码片段可以解决这个问题,但问题是当我显示纹理时它显示为镜像倒置.

int numComponents = 4;

UIImage* image = [UIImage imageNamed:@"Test.png"];
CGImageRef imageRef = [image CGImage];
int width = CGImageGetWidth(imageRef);
int height = CGImageGetHeight(imageRef);

//Allocate texture data
GLubyte* textureData = (GLubyte *)malloc(width * height * numComponents);    

CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
NSUInteger bytesPerPixel = 4;
NSUInteger bytesPerRow = bytesPerPixel * width;
NSUInteger bitsPerComponent = 8;
CGContextRef context = CGBitmapContextCreate(textureData, width, height,
                                             bitsPerComponent, bytesPerRow, colorSpace,
                                             kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big);
CGColorSpaceRelease(colorSpace);

CGContextDrawImage(context, CGRectMake(0, 0, width, height), imageRef);
CGContextRelease(context);

//texture setup
GLuint textureID;    
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
glGenTextures(1, …
Run Code Online (Sandbox Code Playgroud)

ios4 ios opengl-es-2.0

1
推荐指数
1
解决办法
3996
查看次数

CGContextShowTextAtPoint在Retina设备上产生非Retina输出

我正在尝试添加一个文本,UIImage我得到一个像素化的绘图.

我尝试了其他一些没有成功的答案:

我的代码:

-(UIImage *)addText:(UIImage *)imgV text:(NSString *)text1
{
   int w = self.frame.size.width * 2;
   int h = self.frame.size.height * 2;
   CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
   CGContextRef context = CGBitmapContextCreate
         (NULL, w, h, 8, 4 * w, colorSpace, kCGImageAlphaPremultipliedFirst);

   CGContextDrawImage(context, CGRectMake(0, 0, w, h), imgV.CGImage);    
   CGContextSetRGBFillColor(context, 0.0, 0.0, 1.0, 1);

   char* text = (char *)[text1 cStringUsingEncoding:NSASCIIStringEncoding];
   CGContextSelectFont(context, "Arial", 24, kCGEncodingMacRoman);

   // Adjust text;

   CGContextSetTextDrawingMode(context, kCGTextInvisible);
   CGContextShowTextAtPoint(context, 0, 0, text, strlen(text));

   CGPoint pt = CGContextGetTextPosition(context); …
Run Code Online (Sandbox Code Playgroud)

iphone core-graphics ios

1
推荐指数
1
解决办法
960
查看次数