2015-10-27 30 views
0

所以我明白一個UIImage本質上沒有背景。正如我們許多人所知道的,許多PNG文件沒有背景顏色,因此很清楚。我試圖上傳一個沒有背景顏色的PNG文件,因此顏色很清晰。是的,我知道我可以將自己的背景顏色設置爲Adobe或素描,但我假設其他用戶不知道如何執行此操作。我有一個沒有背景的PNG文件,我如何在iOS中爲這個圖像創建一個清晰的顏色背景?

這裏是PNG的截圖,我已經創建了:

screenshot

正如你所看到的,它只是兩行是聯合在一起,所以沒有背景設定。

下面是使用imagePicker從我的照片卷中選擇此png圖像的後果的屏幕截圖。

screenshot

注意的是被認爲是透明的區域實際上是黑色的。我想在黑色部分進行着色,然後使其變爲clearColor,並保持綠色的十字形。現在,我不確定黑色是否實際上甚至是黑色,因爲它可能只是空的空間。我可以填寫空白的黑色空間並將其變爲清晰的顏色嗎?

這裏是我的代碼,現在不工作得非常好:

func overlayImage(image: UIImage, color: UIColor) -> UIImage? { 
    let rect = CGRectMake(0, 0, image.size.width, image.size.height) 
    let backgroundView = UIView(frame: rect) 
    backgroundView.backgroundColor = color 
    UIGraphicsBeginImageContext(rect.size) 

    let gcSize: CGSize = backgroundView.frame.size 
    UIGraphicsBeginImageContext(gcSize) 

    let context: CGContextRef = UIGraphicsGetCurrentContext()! 

    backgroundView.layer.renderInContext(context) 

    let newImage = UIGraphicsGetImageFromCurrentImageContext() 
    UIGraphicsEndImageContext() 

    return newImage 
} 

在任一OBJ-C或斯威夫特任何幫助將不勝感激。

我擺脫上面覆蓋的方法和我用下面的代碼:

與新代碼更新了仍然不能正常工作

func imagePickerController(picker: UIImagePickerController, didFinishPickingImage image: UIImage!, editingInfo: [NSObject : AnyObject]!) { 
    scaleImage(overlayImage(image, color: UIColor.clearColor())) 
} 

func scaleImageAndAddAugmented(image: UIImage?) { 
    let rect = CGRectMake(0, 0, image!.size.width, image!.size.height) 
    let backgroundView = UIView(frame: rect) 
    backgroundView.backgroundColor = UIColor.clearColor() 

    let size = CGSizeApplyAffineTransform(image!.size, CGAffineTransformMakeScale(0.25, 0.25)) 
    let hasAlpha = false 
    let scale: CGFloat = 0.0 

    UIGraphicsBeginImageContextWithOptions(size, !hasAlpha, scale) 
    image!.drawInRect(CGRect(origin: CGPointZero, size: size)) 

    let context = CGBitmapContextCreate(nil, Int(image!.size.width), Int(image!.size.height), 8, 0, CGColorSpaceCreateDeviceRGB(), CGImageAlphaInfo.PremultipliedLast.rawValue) 

    let myGeneratedImage = CGBitmapContextCreateImage(context) 

    CGContextDrawImage(context, rect, myGeneratedImage) 

    let scaledImage = UIGraphicsGetImageFromCurrentImageContext() 
    UIGraphicsEndImageContext() 

    self.dismissViewControllerAnimated(true, completion: nil) 

    // set image below 
} 
+0

你爲什麼不創建一個自定義的交叉png圖片,並將其添加? –

+0

PNG中的黑色部分實際上是否清晰?或者它現在是黑色的,你想清楚嗎? – Stonz2

+0

澄清這是如何使用的,以及哪些值與您在問題中發佈的圖片相關。 – rmaddy

回答

1

這似乎有點多餘的工作,使用UIGraphicsGetImageFromCurrentImageContext從上下文生成圖像時,只需從文件創建圖像即可。

由於第一次提到它需要有不透明= NO:

UIGraphicsBeginImageContextWithOptions(size, NO, 0.0); 

我敢打賭,它的圖像本身,因爲整個事情是褪色。

使100%肯定的是,通過顏色是一個明確的顏色:

backgroundView.backgroundColor = color 

您可以創建一個位圖的上下文並使用它:

CGContextRef context = CGBitmapContextCreate(NULL, 
            width, 
            height, 
            8, 
            0, 
            rgbColorSpace, 
            kCGImageAlphaPremultipliedLast); 

而不是使用當前上下文和您可以使用:

myGeneratedImage = CGBitmapContextCreateImage(context) 

繪圖很容易:

CGContextDrawImage(realContext,bounds,myGeneratedImage) 
+0

嗯有趣。我希望我現在可以嘗試,但遠離電腦。我會盡快告訴你它是如何發生的。 – jasonnoahchoi

+0

我不確定這是否按預期工作,如果您有時間再次查看,我已用更多代碼更新了我的問題。 – jasonnoahchoi

+0

它看起來像alpha沒有正確配置,嗯.. –

0

雨燕下面的代碼:

func scaleImage(image: UIImage?) { 
    if let image = image { 
     let rect = CGRectMake(0, 0, image.size.width, image.size.height) 

     let size: CGSize = CGSizeApplyAffineTransform(image.size, CGAffineTransformMakeScale(0.25, 0.25)) 

     UIGraphicsBeginImageContextWithOptions(size, false, 0.0) 
     image.drawInRect(CGRect(origin: CGPointZero, size: size)) 

     let context = CGBitmapContextCreate(nil, Int(image.size.width), Int(image.size.height), 8, 0, CGColorSpaceCreateDeviceRGB(), CGImageAlphaInfo.PremultipliedLast.rawValue) 

     let myGeneratedImage = CGBitmapContextCreateImage(context) 

     CGContextDrawImage(context, rect, myGeneratedImage) 

     let scaledImage = UIGraphicsGetImageFromCurrentImageContext() 
     UIGraphicsEndImageContext() 
    } 
} 
相關問題