I have a UIImage with white background. I would like replace the white background/pixels with alpha-transparent pixels. I've looked at other questions on StackOverflow, along with Quartz documentation, but have yet to find a coherent "start-to-end" for this problem. How is this done?
Replace all white/nearly white pixels in a UIImage with alpha using CGImage?
1.2k Views Asked by Peter Hajas At
2
There are 2 best solutions below
2
Michael Aaron Safyan
On
The first step is you need to define some sort of "distance" function to determine how far away a pixel is from being white. Then you need to define a distance threshold below which a pixel is considered white. Then you would need to iterate over the pixels of the image, changing any pixels that were considered white according to your distance and threshold, to being transparent. The main trick, though, is making this efficient... touching pixels through functions will be very slow; your best bet is to touch the pixels directl by gaining access to the memory buffer in which the pixels reside and stepping through them.
Related Questions in IPHONE-SDK-3.0
- Access the dynamically loaded Labels in UIScrolView
- Localization for iPhone apps - what's your approach?
- Getting exception when try to PresentModalViewController
- Detect taps of links/numbers in UITextView
- What is Cocoa Studio
- is there any easy way to get Date?
- Writing to iPhone keychaing using apple KeyChainItemWrapper
- Load all the Contacts from iphone PhoneBook in a variable
- iphone Dev. How can I make my app compatible with iOS 3 and iPads at the same time (iOS 4.2)
- Get latitude and longitude for map click
- Help me to play video on iPhone App
- Is it easy to make 2 language versions for iPhone (Japanese)?
- NSMutableAttributedString on iOS 3.1.3
- My developer didn't use Interface Builder, is that a bad thing?
- how to create animation of sparkling effect for iphone using cocos2d or Quartz core
Related Questions in UIIMAGE
- Rotating CIImage around a point
- Swift: Display image from UIImage array in table view
- Trouble in uploading profile image to parse.com during signUpInBackgroundWithBlock
- Slight delay before image blurred
- Get name of img from for loop
- How to store PNG images for profile pictures on Parse.com?
- Allow user to change image to one from their gallery
- Swift: UIImages appearing out of order in table view
- Changing the size of the UIImage
- Loading multiple saved images in app slows it down
- Can't load image with UIImage or NSData
- iOS AVAssetWriter Video Height "Squished" when exported to from UIImages
- How to add an image when a row is selected and then remove it when it is deselected in UITableView in SWIFT
- How to add UIImage object into NSMutableArray in iOS
- WatchKit extension: how to read data from host iOS app?
Related Questions in MASK
- Flash erase image with mask - gotoandplay next frame
- Loading delay extjs
- Masks in JSF2 with rich:jQuery?
- Unity | 'gameobject.renderer.material.color' in version 5.x
- Android PdfRenderer class - Too long to render on few PDFs
- OpenCV & Python: quickly superimpose mask over image without overflow
- How to create rotated rectangular or polygonal ROI/mask?
- Create mask from bwtraceboundary in Matlab
- Swift: Mask Alignment + Auto-Layout Constraints
- MaskedTextBox Currency Input Mask Limits
- Make element transparent through multiple parent elements?
- Mask CALayer with Another CALayer
- Moving part of the canvas Android
- SKCropNode not rendering greyscale mask values in SpriteKit
- Create cameraview (mask) on surfaceview in android
Related Questions in CGIMAGE
- Pixel Array to UIImage in Swift
- iOS overwrite EXIF flash property with new value for front camera flash
- How do I get the color data of 3 points in my sprite kit game?
- CGImage memory size returning weird results
- [NSNull CGImage]: unrecognized selector exception when picking an asset and converting it to JPEG
- Trying to crop my UIImage to a 1:1 aspect ratio (square) but it keeps enlarging the image causing it to be blurry. Why?
- iOS PNG Alpha channel is solid when converting UIImage to CGImage for OpenGL
- CGImageDestinationAddImage writes image without blue elements
- How to decode raw png data from Assimp to CGImage?
- How to apply UIEdgeInsets to CGImage?
- Unable to set frame of masked Image - Objective C
- What could be the cause of this error with CGBitmapContextCreate()?
- Most efficient method of loading a smaller representation of an image
- Objective c: drawing a png on the screen (UIImage inside a UIImageView)
- error with CGImageCreateWithImageProvider
Related Questions in CGIMAGEMASKCREATE
- How to remove the transparent area of an UIImageView after masking?
- Core Graphics CGImage mask not affecting
- Bitmap context for wide color range
- How to make CGImageMaskCreate work on 3.5-inch devices?
- Turning a freehand drawing in a UIImageView into a mask
- Replace all white/nearly white pixels in a UIImage with alpha using CGImage?
- Masking a progression UIImage
- cgimagemaskcreate and memory leak, how to fix it
Trending Questions
- UIImageView Frame Doesn't Reflect Constraints
- Is it possible to use adb commands to click on a view by finding its ID?
- How to create a new web character symbol recognizable by html/javascript?
- Why isn't my CSS3 animation smooth in Google Chrome (but very smooth on other browsers)?
- Heap Gives Page Fault
- Connect ffmpeg to Visual Studio 2008
- Both Object- and ValueAnimator jumps when Duration is set above API LvL 24
- How to avoid default initialization of objects in std::vector?
- second argument of the command line arguments in a format other than char** argv or char* argv[]
- How to improve efficiency of algorithm which generates next lexicographic permutation?
- Navigating to the another actvity app getting crash in android
- How to read the particular message format in android and store in sqlite database?
- Resetting inventory status after order is cancelled
- Efficiently compute powers of X in SSE/AVX
- Insert into an external database using ajax and php : POST 500 (Internal Server Error)
Popular Questions
- How do I undo the most recent local commits in Git?
- How can I remove a specific item from an array in JavaScript?
- How do I delete a Git branch locally and remotely?
- Find all files containing a specific text (string) on Linux?
- How do I revert a Git repository to a previous commit?
- How do I create an HTML button that acts like a link?
- How do I check out a remote Git branch?
- How do I force "git pull" to overwrite local files?
- How do I list all files of a directory?
- How to check whether a string contains a substring in JavaScript?
- How do I redirect to another webpage?
- How can I iterate over rows in a Pandas DataFrame?
- How do I convert a String to an int in Java?
- Does Python have a string 'contains' substring method?
- How do I check if a string contains a specific word?
A UIImage wraps a CGImage. Take the CGImage, run it through CGImageCreateWithMaskingColors, then either create a new UIImage from the result or assign the result back to the UIImage.