Making a UIImage to a circle form
try this code yourImageView.layer.cornerRadius = yourImageView.frame.size.height /2; yourImageView.layer.masksToBounds = YES; yourImageView.layer.borderWidth = 0; this show image like ios 7 circle image thanks
try this code yourImageView.layer.cornerRadius = yourImageView.frame.size.height /2; yourImageView.layer.masksToBounds = YES; yourImageView.layer.borderWidth = 0; this show image like ios 7 circle image thanks
The accepted answer is correct, but there is a much more easy way for UIImageView: Obj-C: UIImage *image = [UIImage imageNamed:@”foo.png”]; theImageView.image = [image imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate]; [theImageView setTintColor:[UIColor redColor]]; Swift 2: let theImageView = UIImageView(image: UIImage(named:”foo”)!.imageWithRenderingMode(UIImageRenderingMode.AlwaysTemplate)) theImageView.tintColor = UIColor.redColor() Swift 3: let theImageView = UIImageView(image: UIImage(named:”foo”)!.withRenderingMode(.alwaysTemplate)) theImageView.tintColor = UIColor.red
First you create a UIImage from your image file, then create a UIImageView from that: let imageName = “yourImage.png” let image = UIImage(named: imageName) let imageView = UIImageView(image: image!) Finally you’ll need to give imageView a frame and add it your view for it to be visible: imageView.frame = CGRect(x: 0, y: 0, width: 100, … Read more
Thats because UIBarButtonItem image’s default rendering mode always draw the image as a template image, ignoring its color information (UIImageRenderingModeAlwaysTemplate). Just create your image using UIImage’s method imageWithRenderingMode always original. UIImage(named: “yourImageName”)!.withRenderingMode(.alwaysOriginal)
OK – at last I seem to have done it. Any comments on the correctness would be useful… needed a translate, a rotate, a scale and an offset from the drawing rect position to make it work. Code is here: CGAffineTransform transform = CGAffineTransformIdentity; transform = CGAffineTransformTranslate(transform, boundingRect.size.width/2, boundingRect.size.height/2); transform = CGAffineTransformRotate(transform, angle); transform = … Read more
Not at all … is the answer to the original question: How necessary is it to search for a path to an image using the NSBundle method pathForResource when creating a UIImage using imageNamed? Not much .. is how correct the accepted answer from Zoul and the other one from Ranga are. To be fair: … Read more
In response to Slee’s question above, this is the method I use: -(BOOL)dataIsValidJPEG:(NSData *)data { if (!data || data.length < 2) return NO; NSInteger totalBytes = data.length; const char *bytes = (const char*)[data bytes]; return (bytes[0] == (char)0xff && bytes[1] == (char)0xd8 && bytes[totalBytes-2] == (char)0xff && bytes[totalBytes-1] == (char)0xd9); }
If you are using iOS6, Fernando’s answer will not work, because the saveImage selector is no longer available. The process is pretty confusing, and I have not seen any clear answers posted, so here is the method I’ve used to solve this in iOS6. You will need to use a combination of the following: Create … Read more
From: pulling an UIImage from a UITextView or UILabel gives white image. // iOS – (UIImage *)grabImage { // Create a “canvas” (image context) to draw in. UIGraphicsBeginImageContextWithOptions(self.bounds.size, self.opaque, 0.0); // high res // Make the CALayer to draw in our “canvas”. [[self layer] renderInContext: UIGraphicsGetCurrentContext()]; // Fetch an UIImage of our “canvas”. UIImage *image … Read more
UIImage(data:imageData,scale:1.0) presuming the image’s scale is 1. In swift 4.2, use below code for get Data(). image.pngData()