简体   繁体   English

创建特定大小的 UIImage

[英]Create UIImage of a specific size

How can I create a UIImage from scratch.如何从头开始创建 UIImage。 Specifically, I want to create a UIImage of size 320x50.具体来说,我想创建一个大小为 320x50 的 UIImage。 Then, I'd like to be able to draw polygons of specific color onto that image.然后,我希望能够在该图像上绘制特定颜色的多边形。

Here is an answer for you stitch picture in iphone这是您在 iphone 中拼接图片的答案

And base on my experience, I can give you some note:根据我的经验,我可以给你一些提示:

Propotional 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;
}

Resize调整大小

- (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;
}

Handle specific view处理特定视图

You hava to import QuzrtzCore.framework first你必须先导入 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;
}

Handle a range form image处理范围表单图像

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 the image保存图像

Save in app保存在应用程序中

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

Save in album保存到相册

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

You can:你可以:

  1. Create a CGBitmapContext using the color/pixel/dimensions/etc you will need.使用您需要的颜色/像素/尺寸/等创建一个CGBitmapContext
  2. Use the context or manipulate pixels directly.使用上下文或直接操作像素。
  3. Create a UIImage using the result of CGBitmapContextCreateImage使用CGBitmapContextCreateImage的结果创建一个UIImage

I leave here my answer, just in case this can help in the future.我把我的答案留在这里,以防万一这对将来有帮助。 This answer is valid on iOS10+此答案在 iOS10+ 上有效

extension UIImage {
    static func image(with size: CGSize) -> UIImage {
        UIGraphicsImageRenderer(size: size)
            .image { $0.fill(CGRect(origin: .zero, size: size)) }
    }
}

so you can do: UIImage.image(with: CGSize(width: 320, height: 50))所以你可以这样做: UIImage.image(with: CGSize(width: 320, height: 50))

My answer is inspired by this: https://stackoverflow.com/a/48441178/2000162我的回答是受此启发: https : //stackoverflow.com/a/48441178/2000162

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM