CVPixelBufferRef to NSImage - macos

How can I convert a CVPixelBufferRef to an NSImage or CGImageRef? I need to be able to display the contents of the pixel buffer in a Core Animation Layer.

To convert the buffer to an image, you need to first convert the buffer to an CIImage, which then can be converted to an NSImage.
let ciImage = CIImage(cvImageBuffer: pixelBuffer)
let context = CIContext(options: nil)
From here you can go to both GCImage and NSImage:
let width = CVPixelBufferGetWidth(pixelBuffer)
let height = CVPixelBufferGetHeight(pixelBuffer)
let cgImage = context.createCGImage(ciImage, from: CGRect(x: 0, y: 0, width: width, height: height))
let nsImage = NSImage(cgImage: cgImage, size: CGSize(width: width, height: height))

Related

UISlider Thumb Image gets pixelated even if I use .svg file

Why is my custom thumb image gets blurry or pixelated even if I use svg file. When I use the image on a button, it works fine. Please help. Here's my code
func resizeImage(image: UIImage, targetSize: CGSize) -> UIImage? {
let size = image.size
let widthRatio = targetSize.width / size.width
let heightRatio = targetSize.height / size.height
// Figure out what our orientation is, and use that to form the rectangle
var newSize: CGSize
if(widthRatio > heightRatio) {
newSize = CGSize(width: size.width * heightRatio, height: size.height * heightRatio)
} else {
newSize = CGSize(width: size.width * widthRatio, height: size.height * widthRatio)
}
// This is the rect that we've calculated out and this is what is actually used below
let rect = CGRect(origin: .zero, size: newSize)
// Actually do the resizing to the rect using the ImageContext stuff
UIGraphicsBeginImageContextWithOptions(newSize, false, 1.0)
image.draw(in: rect)
let newImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return newImage
}
and then I call it in my viewDidLoad():
let resized = resizeImage(image: (UIImage(named: "add_svg")!), targetSize: CGSize(width: 100.0, height: 100.0))
slider.setThumbImage(resized, for: .normal)
See below photo. the upper image is a button, the below image (slightly blurred) is the thumb image of uislider. Thanks in advance!
enter image description here

Fade in/out Image in UIIamgeView happens with no duration

I have a UIIamge I created programmatically in viewDidLoad of a DetailViewController. I would like to .transitionCrossDissolve between its current image to another image. My problem is that once the DetailViewCOntroller is displayed the switch happens right away no matter the duration. I never even see the first image. I am using the following code.
let rect = CGRect(x: 0, y: UIApplication.shared.statusBarFrame.height, width: view.frame.size.width, height: view.frame.size.height / 3.0)
let imageView = UIImageView(frame: rect)
imageView.image = #imageLiteral(resourceName: "head1")
imageView.contentMode = .scaleAspectFill
imageView.clipsToBounds = true
view.addSubview(imageView)
UIView.transition(with: imageView,
duration: 16.0,
options: .transitionCrossDissolve,
animations: { imageView.image = #imageLiteral(resourceName: "head3") },
completion: nil)
Try to start the animation in the viewDidAppear delegate. Think thats the problem.

Swift 3 Colour Space macOS not IOS

How can I convert an RGB image into its grayscaled colour space? I can find a lot of code for iOS but non for macOS.. And the Apple's documentations are all in objective C....
let width = image.size.width
let height = image.size.height
let imageRect = NSMakeRect(0, 0, width, height);
let colorSpace = CGColorSpaceCreateDeviceGray();
let bits = image.representations.first as! NSBitmapImageRep;
bitmap!.representationUsingType(NSBitmapImageFileType.NSPNGFileType, properties: nil)
let bitmapInfo = CGBitmapInfo(rawValue: CGImageAlphaInfo.none.rawValue);
let context = CGContext(data: nil, width: Int(width), height: Int(height), bitsPerComponent: 8, bytesPerRow: 0, space: colorSpace, bitmapInfo: bitmapInfo.rawValue);
context.draw(image.cgImage!, in : imageRect);// and this line is wrong obviously..
This is what I have got so far..just copy and pasting from the internet.. but I have no idea on how to go further...
I have found an interesting way to do this.. My code are simply copied from the three sources below.
how to create grayscale image from nsimage in swift?
Greyscale Image using COCOA and NSImage
Changing the Color Space of NSImage: The second reply
My Code:
func saveImage(image:NSImage, destination:URL) throws{
let rep = greyScale(image);
var data = rep.representation(using: NSJPEGFileType, properties: [:]);
try data?.write(to: destination);
}
// rgb2gray
func greyScale(image: NSImage) -> NSBitmapImageRep{
let w = image.size.width
let h = image.size.height
let imageRect : NSRect! = NSMakeRect(0,0, w, h);
let colourSpace : ColourSpace! = CGColorSpaceCreateDeviceGray();
let context : CGContext! = CGContext(data: nil, width: Int(w),
height: Int(h), bitsPerComponent: 8,
bytesPerRow: 0, space: colorSpace,
bitmapInfo: CGImageAlphaInfo.none.rawValue);
context.draw(nsImageToCGImage(image: image), in: imageRect);
let greyImage : CGImage! = context.makeImage();
return NSBitmapImageRep(cgImage: greyImage);
}
func nsImageToCGImage(image: NSImage) -> CGImage{
if let imageData = image.tiffRepresentation as NSData! {
let imageSource : CGImageSource! = CGImageSourceCreateWithData(imageData,
nil);
let image = CGImageSourceCreateImageAtIndex(imageSource, 0, nil);
return image;
}
return nil;
}
I am still trying to understand the principle behind.
You can try CIFilter. The annoyance is that you have to convert back and forth between NSImage and CIImage:
import Cocoa
import CoreImage
let url = Bundle.main.url(forResource: "image", withExtension: "jpg")!
let image = CIImage(contentsOf: url)!
let bwFilter = CIFilter(name: "CIColorControls", withInputParameters: ["inputImage": image, "inputSaturation": 0.0])!
if let ciImage = bwFilter.outputImage {
let rep = NSCIImageRep(ciImage: ciImage)
let nsImage = NSImage(size: rep.size)
nsImage.addRepresentation(rep)
// nsImage is now your black-and-white image
}

Resize UIImageView based on image with respect to UIImageViewWidth

I have a UIImageView that is the width of the entire screen and the height is 400 pixels.
The end result I am looking for is that every single image has the exact same width (the screen width) and the height is adjusted to accommodate this while keeping its aspect ratio.
So if an image is 400 pixels wide, it needs to reduce to 320 pixels wide, and the height of the image view should adjust and become SHORTER to keep the ratio.
If an image is 240 pixels wide, it needs to increase its width to 320 and adjust the hight to be TALLER to keep the ratio.
I have been looking through many posts that all seem to just point to setting the content mode to aspect fit, but this does nothing like what I am looking for.
Any help would be great, thanks!
So it looks like shortly after I posted it, I checked storyboard, and for some reason the code was not overwriting the storyboard.
If I change it to Aspect Fit in storyboard, it actually functions the way it is supposed to.
::face palm::
You just need to set the content mode property to Aspect Fit in your imageview.
UIImage *originalImage = [UIImage imageNamed:#"xxx.png"];
double width = originalImage.size.width;
double height = originalImage.size.height;
double apectRatio = width/height;
//You can mention your own width like 320.0
double newHeight = [[UIScreen mainScreen] bounds].size.width/ apectRatio;
self.img.frame = CGRectMake(0, 0, [[UIScreen mainScreen] bounds].size.width, newHeight);
self.img.center = self.view.center;
self.img.image = originalImage;
func resizeImage(image: UIImage, targetSize: CGSize) -> UIImage {
let size = image.size
let widthRatio = targetSize.width / image.size.width
let heightRatio = targetSize.height / image.size.height
// Figure out what our orientation is, and use that to form the rectangle
var newSize: CGSize
if(widthRatio > heightRatio) {
newSize = CGSize(width: size.width * heightRatio, height: size.height * heightRatio)
} else {
newSize = CGSize(width: size.width * widthRatio, height: size.height * widthRatio)
}
// This is the rect that we've calculated out and this is what is actually used below
let rect = CGRect(x: 0, y: 0, width: newSize.width, height: newSize.height)
// Actually do the resizing to the rect using the ImageContext stuff
UIGraphicsBeginImageContextWithOptions(newSize, false, 1.0)
image.draw(in: rect)
let newImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return newImage!
}
Now get the resized image from the original image, as I done it like:
let image = UIImage(named: "YOUR IMAGE NAME")
let newHeight = (image?.size.height/image?.size.width) * YOUR_UIIMAGE_VIEW_WIDTH
let newSize = CGSize(width: YOUR_UIIMAGE_VIEW_WIDTH, height: newHeight)
let newResizedImage = resizeImage(image: image, targetSize: newSize)
Hope, this will help.

Xcode split image into 4 [duplicate]

How would I go about splitting a UIImage In half(down the middle) so it would make two images?
You can try this,
UIImage *image = [UIImage imageNamed:#"yourImage.png"];
CGImageRef tmpImgRef = image.CGImage;
CGImageRef topImgRef = CGImageCreateWithImageInRect(tmpImgRef, CGRectMake(0, 0, image.size.width, image.size.height / 2.0));
UIImage *topImage = [UIImage imageWithCGImage:topImgRef];
CGImageRelease(topImgRef);
CGImageRef bottomImgRef = CGImageCreateWithImageInRect(tmpImgRef, CGRectMake(0, image.size.height / 2.0, image.size.width, image.size.height / 2.0));
UIImage *bottomImage = [UIImage imageWithCGImage:bottomImgRef];
CGImageRelease(bottomImgRef);
hope this can help you, :)
- (void)splitImage:(UIImage *)image
{
CGFloat imgWidth = image.size.width/2;
CGFloat imgheight = image.size.height;
CGRect leftImgFrame = CGRectMake(0, 0, imgWidth, imgheight);
CGRect rightImgFrame = CGRectMake(imgWidth, 0, imgWidth, imgheight);
CGImageRef left = CGImageCreateWithImageInRect(image.CGImage, leftImgFrame);
CGImageRef right = CGImageCreateWithImageInRect(image.CGImage, rightImgFrame);
// These are the images we want!
UIImage *leftImage = [UIImage imageWithCGImage:left];
UIImage *rightImage = [UIImage imageWithCGImage:right];
// Don't forget to free the memory!
CGImageRelease(left);
CGImageRelease(right);
}
iOS 11, swift 4.0 updated version https://stackoverflow.com/users/893872/durul-dalkanat which I think to be honest is the best :)
This splits an image into 4 parts
func splitImage(image2D: UIImage) -> [UIImage] {
let imgWidth = image2D.size.width / 2
let imgHeight = image2D.size.height / 2
var imgImages:[UIImage] = []
let leftHigh = CGRect(x: 0, y: 0, width: imgWidth, height: imgHeight)
let rightHigh = CGRect(x: imgWidth, y: 0, width: imgHeight, height: imgHeight)
let leftLow = CGRect(x: 0, y: imgHeight, width: imgWidth, height: imgHeight)
let rightLow = CGRect(x: imgWidth, y: imgHeight, width: imgWidth, height: imgHeight)
let leftQH = image2D.cgImage?.cropping(to:leftHigh)
let rightHQ = image2D.cgImage?.cropping(to:rightHigh)
let leftQL = image2D.cgImage?.cropping(to:leftLow)
let rightQL = image2D.cgImage?.cropping(to:rightLow)
imgImages.append(UIImage(cgImage: leftQH!))
imgImages.append(UIImage(cgImage: rightHQ!))
imgImages.append(UIImage(cgImage: leftQL!))
imgImages.append(UIImage(cgImage: rightQL!))
return imgImages
}

Resources