How to compress of reduce the size of an image before uploading to Parse as PFFile? (Swift)

Yes you can use UIImageJPEGRepresentation instead of UIImagePNGRepresentation to reduce your image file size. You can just create an extension UIImage as follow: Xcode 8.2 • Swift 3.0.2 extension UIImage { enum JPEGQuality: CGFloat { case lowest = 0 case low = 0.25 case medium = 0.5 case high = 0.75 case highest = 1 … Read more

How can I mask a UIImageView?

There’s an easier way. #import <QuartzCore/QuartzCore.h> // remember to include Framework as well CALayer *mask = [CALayer layer]; mask.contents = (id)[[UIImage imageNamed:@”mask.png”] CGImage]; mask.frame = CGRectMake(0, 0, <img_width>, <img_height>); yourImageView.layer.mask = mask; yourImageView.layer.masksToBounds = YES; For Swift 4 and plus follow code below let mask = CALayer() mask.contents = UIImage(named: “right_challenge_bg”)?.cgImage as Any mask.frame = … Read more

Changing UIImage color

Since iOS 7, this is the most simple way of doing it. Objective-C: theImageView.image = [theImageView.image imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate]; [theImageView setTintColor:[UIColor redColor]]; Swift 2.0: theImageView.image = theImageView.image?.imageWithRenderingMode(.AlwaysTemplate) theImageView.tintColor = UIColor.magentaColor() Swift 4.0: theImageView.image = theImageView.image?.withRenderingMode(.alwaysTemplate) theImageView.tintColor = .magenta Storyboard: First configure the image as template ( on right bar – Render as) in your assets. Then the … Read more

How do I save a UIImage to a file?

Of course you can create subfolders in the documents folder of your app. You use NSFileManager to do that. You use UIImagePNGRepresentation to convert your image to NSData and save that to disk. // Create path. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *filePath = [[paths objectAtIndex:0] stringByAppendingPathComponent:@”Image.png”]; // Save image. [UIImagePNGRepresentation(image) writeToFile:filePath atomically:YES]; Core … Read more

How can I color a UIImage in Swift?

Swift 4 and 5 extension UIImageView { func setImageColor(color: UIColor) { let templateImage = self.image?.withRenderingMode(.alwaysTemplate) self.image = templateImage self.tintColor = color } } Call like this: let imageView = UIImageView(image: UIImage(named: “your_image_name”)) imageView.setImageColor(color: UIColor.purple) Alternativ For Swift 3, 4 or 5 extension UIImage { func maskWithColor(color: UIColor) -> UIImage? { let maskImage = cgImage! let … Read more

How to Resize image in Swift?

See my blog post, Resize image in swift and objective C, for further details. Image resize function in swift as below. 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 … Read more

PhotoPicker discovery error: Error Domain=PlugInKit Code=13

You need to make explicit Objective-C reference: @objc @objc func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) { let chosenImage = info[UIImagePickerControllerOriginalImage] as! UIImage image = chosenImage self.performSegue(withIdentifier: “ShowEditView”, sender: self) dismiss(animated: true, completion: nil) }

How to create a colored 1×1 UIImage on the iPhone dynamically?

You can use CGContextSetFillColorWithColor and CGContextFillRect for this: Swift extension UIImage { class func image(with color: UIColor) -> UIImage { let rect = CGRectMake(0.0, 0.0, 1.0, 1.0) UIGraphicsBeginImageContext(rect.size) let context = UIGraphicsGetCurrentContext() CGContextSetFillColorWithColor(context, color.CGColor) CGContextFillRect(context, rect) let image = UIGraphicsGetImageFromCurrentImageContext() UIGraphicsEndImageContext() return image } } Swift3 extension UIImage { class func image(with color: UIColor) -> … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)