0

基本上我有一个 main UIImage,它充当背景/边框。其中UIImage我还有 2 个UIImages,垂直分割,它们周围有一个间隙,所以你仍然可以看到主背景的边框UIImage。在每一面我都有一个UILabel, 来描述图像。下面是我想要帮助理解的图片。

在此处输入图像描述

我想要实现的是将其制作成 1 张图像,但保持所有当前位置、布局、图像布局(方面填充)和标签大小和标签背景颜色相同。我也希望这张图片具有相同的质量,所以它看起来仍然不错。

我查看了许多其他 stackoverflow 问题,到目前为止提出了以下问题,但它存在以下问题:

  • 没有将图像定位labels到正确的位置和大小
  • 没有标签或主图像的背景颜色
  • 没有图像作为 Aspect Fill(如 UIImageViews),因此每张图片的外部也会显示并且没有正确裁剪,就像上面的示例一样。

以下是我到目前为止的代码,有人可以像上图那样帮我实现吗?我对 iOS 开发相当陌生,并且有点挣扎:

-(UIImage *)renderImagesForSharing{

    CGSize newImageSize = CGSizeMake(640, 640);
    NSLog(@"CGSize %@",NSStringFromCGSize(newImageSize));

    UIGraphicsBeginImageContextWithOptions(newImageSize, NO, 0.0);

    [self.mainImage.layer renderInContext:UIGraphicsGetCurrentContext()];
    [self.beforeImageSide.image drawInRect:CGRectMake(0,0,(newImageSize.width/2),newImageSize.height)];
    [self.afterImageSize.image drawInRect:CGRectMake(320,0,(newImageSize.width/2),newImageSize.height) blendMode:kCGBlendModeNormal alpha:1.0];

    [self.beforeLabel drawTextInRect:CGRectMake(60.0f, 0.0f, 200.0f, 50.0f)];
    [self.afterLabel drawTextInRect:CGRectMake(0.0f, 0.0f, 100.0f, 50.0f)];


    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();

    NSData *imgData =  UIImageJPEGRepresentation(image, 0.9);
    UIImage * imagePNG = [UIImage imageWithData:imgData]; //wrap UIImage around PNG representation

    UIGraphicsEndImageContext();
    return imagePNG;
}

提前感谢您的任何帮助!

4

2 回答 2

2

我不明白您为什么要使用drawInRect:它来完成此任务。由于您拥有图像和所有东西,因此您可以轻松创建图像中显示的视图。然后像这样截取它:

UIGraphicsBeginImageContextWithOptions(self.view.bounds.size, self.view.opaque, 0.0);
[self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage*theImage=UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

NSData*theImageData=UIImageJPEGRepresentation(theImage, 1.0 ); //you can use PNG too
[theImageData writeToFile:@"example.jpeg" atomically:YES];

更改为self.view刚刚创建的视图

于 2014-07-24T04:37:59.327 回答
0

它会给一些想法。

UIGraphicsBeginImageContextWithOptions(DiagnosisView.bounds.size, DiagnosisView.opaque, 0.0);
CGContextRef ctx = UIGraphicsGetCurrentContext();
[[UIColor redColor] set];
CGContextFillRect(ctx, DiagnosisView.frame);
[DiagnosisView.layer renderInContext:ctx];
UIImage * img = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

NSString *imagePath = [KdiagnosisFolderPath stringByAppendingPathComponent:FileName];
NSData *pngData = UIImagePNGRepresentation(img);
[pngData writeToFile:imagePath atomically:YES];
pngData = nil,imagePath = nil;
于 2014-07-24T05:09:17.667 回答