Create a UIImage of a specific size

How can I create a UIImage from scratch. In particular, I want to create a UIImage with a size of 320x50. Then I would like to draw polygons of a certain color on this image.

+4
source share
2 answers

Here's the answer for drawing a stitch on the iphone

And based on my experience, I can tell you something:

Proportional scale

  - (UIImage *) scaleImage: (UIImage *) image toScale: (float) scaleSize
 {
  UIGraphicsBeginImageContext (CGSizeMake (image.size.width * scaleSize, image.size.height * scaleSize);
  [image drawInRect: CGRectMake (0, 0, image.size.width * scaleSize, image.size.height * scaleSize)];
  UIImage * scaledImage = UIGraphicsGetImageFromCurrentImageContext ();
  UIGraphicsEndImageContext ();
  return scaledImage;
 }

Change of size

  - (UIImage *) reSizeImage: (UIImage *) image toSize: (CGSize) reSize
 {
  UIGraphicsBeginImageContext (CGSizeMake (reSize.width, reSize.height));
  [image drawInRect: CGRectMake (0, 0, reSize.width, reSize.height)];
  UIImage * reSizeImage = UIGraphicsGetImageFromCurrentImageContext ();
  UIGraphicsEndImageContext ();
  return reSizeImage;
 }

Certain kind of processing

You first import QuzrtzCore.framework

  - (UIImage *) captureView: (UIView *) theView
 {
  CGRect rect = theView.frame; 
  UIGraphicsBeginImageContext (rect.size); 
  CGContextRef context = UIGraphicsGetCurrentContext (); 
  [theView.layer renderInContext: context]; 
  UIImage * img = UIGraphicsGetImageFromCurrentImageContext (); 
  UIGraphicsEndImageContext (); 
  return img;
 }

Range Shape Image Processing

  CGRect captureRect = yourRect
 CGRect viewRect = self.view.frame;
 UIImage * viewImg;
 UIImage * captureImg;

 UIGraphicsBeginImageContext (viewRect.size); 
 CGContextRef context = UIGraphicsGetCurrentContext (); 
 [self.view.layer renderInContext: context]; 
 viewImg = UIGraphicsGetImageFromCurrentImageContext (); 
 UIGraphicsEndImageContext ();

 captureImg = [UIImage imageWithCGImage: CGImageCreateWithImageInRect (viewImg.CGImage, captureRect)];

Save image

Save in application

  NSString * path = [[NSHomeDirectory () stringByAppendingPathComponent: @ "Documents"] stringByAppendingPathComponent: @ "image.png"];
 [UIImagePNGRepresentation (image) writeToFile: path atomically: YES];

Save album

  CGImageRef screen = UIGetScreenImage ();
 UIImage * image = [UIImage imageWithCGImage: screen];
 CGImageRelease (screen);
 UIImageWriteToSavedPhotosAlbum (image, self, nil, nil);
+5
source

You can:

  • Create a CGBitmapContext using the color / pixel / dimensions / etc you need.
  • Use context or manipulate pixels directly.
  • Create a UIImage using the result of CGBitmapContextCreateImage
+2
source

Source: https://habr.com/ru/post/1391213/


All Articles