Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Draw another image on a UIImage

Tags:

Is it possible to add another, smaller, image to a UIImage/UIImageView? If so, how? If not, then how can I draw a small filled triangle?

Thanks

like image 620
Alede Avatar asked Jul 11 '11 21:07

Alede


People also ask

How do I add an image to UIImage?

stroke() let img = UIGraphicsGetImageFromCurrentImageContext() UIGraphicsEndImageContext() testImg = UIImageView(image: img) testImg.

What is the difference between a UIImage and a UIImageView?

UIImage contains the data for an image. UIImageView is a custom view meant to display the UIImage .

How do you add a UIImage in Objective C?

For example: UIImage *img = [[UIImage alloc] init]; [img setImage:[UIImage imageNamed:@"anyImageName"]]; My UIImage object is declared in . h file and allocated in init method, I need to set image in viewDidLoad method.


2 Answers

You could add a subview to your UIImageView containing another image with the small filled triangle. Or you could draw inside of the first image:

CGFloat width, height; UIImage *inputImage;    // input image to be composited over new image as example  // create a new bitmap image context at the device resolution (retina/non-retina) UIGraphicsBeginImageContextWithOptions(CGSizeMake(width, height), YES, 0.0);          // get context CGContextRef context = UIGraphicsGetCurrentContext();         // push context to make it current  // (need to do this manually because we are not drawing in a UIView) UIGraphicsPushContext(context);                               // drawing code comes here- look at CGContext reference // for available operations // this example draws the inputImage into the context [inputImage drawInRect:CGRectMake(0, 0, width, height)];  // pop context  UIGraphicsPopContext();                               // get a UIImage from the image context- enjoy!!! UIImage *outputImage = UIGraphicsGetImageFromCurrentImageContext();  // clean up drawing environment UIGraphicsEndImageContext(); 

This code (source here) will create a new UIImage that you can use to initialize a UIImageView.

like image 173
sergio Avatar answered Sep 20 '22 19:09

sergio


You can try this, works perfect for me, it's UIImage category:

- (UIImage *)drawImage:(UIImage *)inputImage inRect:(CGRect)frame {     UIGraphicsBeginImageContextWithOptions(self.size, NO, 0.0);     [self drawInRect:CGRectMake(0.0, 0.0, self.size.width, self.size.height)];     [inputImage drawInRect:frame];     UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();     UIGraphicsEndImageContext();     return newImage; } 

or Swift:

extension UIImage {     func image(byDrawingImage image: UIImage, inRect rect: CGRect) -> UIImage! {         UIGraphicsBeginImageContext(size)         draw(in: CGRect(x: 0, y: 0, width: size.width, height: size.height))         image.draw(in: rect)         let result = UIGraphicsGetImageFromCurrentImageContext()         UIGraphicsEndImageContext()         return result     } } 
like image 22
iiFreeman Avatar answered Sep 19 '22 19:09

iiFreeman