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

How can I change image tintColor in iOS and WatchKit

iOS For an iOS app, in Swift 3, 4 or 5: theImageView.image = theImageView.image?.withRenderingMode(.alwaysTemplate) theImageView.tintColor = UIColor.red For Swift 2: theImageView.image = theImageView.image?.imageWithRenderingMode(UIImageRenderingMode.AlwaysTemplate) theImageView.tintColor = UIColor.redColor() Meanwhile, the modern Objective-C solution is: theImageView.image = [theImageView.image imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate]; [theImageView setTintColor:[UIColor redColor]]; Watchkit In WatchKit for Apple Watch apps, you can set the tint color for a template … Read more

How to save picture to iPhone photo library?

You can use this function: UIImageWriteToSavedPhotosAlbum(UIImage *image, id completionTarget, SEL completionSelector, void *contextInfo); You only need completionTarget, completionSelector and contextInfo if you want to be notified when the UIImage is done saving, otherwise you can pass in nil. See the official documentation for UIImageWriteToSavedPhotosAlbum().

How do I resize the UIImage to reduce upload image size

Xcode 9 • Swift 4 or later edit/update: For iOS10+ We can use UIGraphicsImageRenderer. For older Swift syntax check edit history. extension UIImage { func resized(withPercentage percentage: CGFloat, isOpaque: Bool = true) -> UIImage? { let canvas = CGSize(width: size.width * percentage, height: size.height * percentage) let format = imageRendererFormat format.opaque = isOpaque return UIGraphicsImageRenderer(size: … Read more

UIImage(contentsOfFile:) returning nil despite file existing in caches directory [duplicate]

The problem there is that you are using URL property absoluteString where you should be using the path property. The difference between absoluteString and path properties is that absoluteString includes the file url scheme (“file://”) which is the reason it doesn’t find the file at what was supposed to be its path but it is … Read more

How to Rotate a UIImage 90 degrees?

I believe the easiest way (and thread safe too) is to do: //assume that the image is loaded in landscape mode from disk UIImage * landscapeImage = [UIImage imageNamed:imgname]; UIImage * portraitImage = [[UIImage alloc] initWithCGImage: landscapeImage.CGImage scale: 1.0 orientation: UIImageOrientationRight]; Note: As Brainware said this only modifies the orientation data of the image – … Read more

Convert between UIImage and Base64 string

Swift First we need to have image’s NSData //Use image name from bundle to create NSData let image : UIImage = UIImage(named:”imageNameHere”)! //Now use image to create into NSData format let imageData:NSData = UIImagePNGRepresentation(image)! //OR next possibility //Use image’s path to create NSData let url:NSURL = NSURL(string : “urlHere”)! //Now use image to create into … Read more