image compression by size – iPhone SDK

Heres some example code that will attempt to compress an image for you so that it doesn’t exceed either a max compression or maximum file size CGFloat compression = 0.9f; CGFloat maxCompression = 0.1f; int maxFileSize = 250*1024; NSData *imageData = UIImageJPEGRepresentation(yourImage, compression); while ([imageData length] > maxFileSize && compression > maxCompression) { compression -= … Read more

PNG/JPEG representation from CIImage always returns nil

Just begin a new graphics context and draw your grayscale image there. iOS 10 or later you can use UIGraphicsImageRenderer, for older iOS version syntax please check edit history: Xcode 11 • Swift 5.1 func blackWhiteImage(image: UIImage, isOpaque: Bool = false) -> Data? { guard let ciImage = CIImage(image: image)?.applyingFilter(“CIColorControls”, parameters: [kCIInputSaturationKey: 0]) else { … Read more

How to zoom in/out an UIImage object when user pinches screen?

As others described, the easiest solution is to put your UIImageView into a UIScrollView. I did this in the Interface Builder .xib file. In viewDidLoad, set the following variables. Set your controller to be a UIScrollViewDelegate. – (void)viewDidLoad { [super viewDidLoad]; self.scrollView.minimumZoomScale = 0.5; self.scrollView.maximumZoomScale = 6.0; self.scrollView.contentSize = self.imageView.frame.size; self.scrollView.delegate = self; } You … Read more