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
- How i can set the auto print symenteneously 3-4 prints on epson tm-m30 printer
- How to encrypt data in objective c and decrypt in mac using terminal
- UIImagePicker shows black space at bottom after dismissing it
- Set Minimum Zoom Level For MKMapView in iOS
- How to drag and reorder UITableViewCell from one UITableView to another similar to trello app?
- Communication over wifi or wirless access points in iOS device
- Audio Visualizer with AVQueuePlayer
- objective-C creating an app with GPS and specific shop finder
- Get facebook events around location
- ImagePicker Crash - select image from library and set in an UIImageView when use AutoLayout
- bringSubviewToFront + setFrame doesn't work
- How to get the proper URL from this tag value?
- Round top corners in UITableView
- How to include FFMpeg library in iphone project
- Convert Hexadecimal to NSData
Related Questions in UIIMAGE
- UIImage Resizing Works, but Reverts to Original Size When Converted to Data for Upload
- Fail to Upload an Image into Firebase Storage
- Is there a way to actually change the background color in UIImage.SymbolConfiguration of a system image? Or is it always clear?
- Check in UIImage.init existence of image in bundle
- How to compress Image without losing metadata?
- setup all of the properties of the stack view
- Changing the color UIImage
- UIImage in Swift only loads after triggering twice
- Cannot generate image which is then shared
- Memory leak issue trying to combine [UIImage] to single GIF image
- Padding around UIImage in a UIImageView with a background color
- Add an image to a subclass of UIVIew
- How to resize image to have maximum width of 1280px
- How to prompt for access again after UIImageWriteToSavedPhotosAlbum in Swift
- Convert a String to Data in Swift
Related Questions in MASK
- Clip image inside of enclosed letters
- How to use filter blur with masked element?
- Using a mask for two elements to view a gradient behind those elements
- Using setTo in OpenCV with CV_32F images
- Determining the appropriate color range on a mask
- My mask image is a transparent only in Safari
- Safari css mask rendering issue
- When using UIScrollView, how can I fade the left and right edges if content is outside the visible bounds?
- Creating a currency mask in vuetify 2 and v-mask
- Extracting High & Low 16 bit Words from a 32 bit number
- Is it possible to make objects (annotated) visible and the background (unannotated) black or white using roboflow?
- Css mask or multiple background images top middle bottom - responsive
- Wavy mask gradient bug with straight lane
- JsonForms update other renderer based on api call
- Is there any way to export polygon coordinates of the mask of a Yolo v7 model?
Related Questions in CGIMAGE
- UIImage Replace Color with Background Image Washing Out and Overlaying everything Instead of Replacing
- Why CGImage size changed after resize with transparent background? And how to lower the memory cost?
- SwiftUI - How to crop the original image (using CGImage) after scaling and moving it?
- How to save an array of CGImages to a mp4 file
- Creating UIImage with Data gives wrong CGImage resolution
- Crop screenshot image with drawn rectangle
- The same swift app generates different images from one video on emulator and on a real device. generateCGImagesAsynchronously
- Convert white pixels to transparency in NSImage / Swift - how to improve?
- ScreenCaptureKit/CVPixelBuffer format yields unexpected results
- How to convert VNRectangleObservation item to UIImage in SwiftUI
- CGImageProperties / CGImageMetadata does not work for a created UIImage
- Add additional information to a UIImage
- Create CGImage from HEIF file
- get the cgImage from a UIImage that was selected using UIImagePickerController
- Unable to generate cgImage from UIGraphics
Related Questions in CGIMAGEMASKCREATE
- Core Graphics CGImage mask not affecting
- Bitmap context for wide color range
- Masking a progression UIImage
- How to remove the transparent area of an UIImageView after masking?
- How to make CGImageMaskCreate work on 3.5-inch devices?
- Turning a freehand drawing in a UIImageView into a mask
- cgimagemaskcreate and memory leak, how to fix it
- Replace all white/nearly white pixels in a UIImage with alpha using CGImage?
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 # Hahtags
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.