ios - 在一个 uiview 中合并多个 View

标签 ios uiimageview

在我的应用程序中,我必须拍照并在图片上添加以下信息,这些信息是:

  • 天气预报
  • 温度
  • GPS 位置

到目前为止,我是通过使用 GPS 和天气预报网络服务(开放天气图)获得这些信息的。我是这样做的:

  1. 我用标准的 UIImagePicker 拍照
  2. 我在我的界面上放了一个按钮来向用户显示图片

当用户按下按钮时,应用程序会打开一个新的 ViewController,我在其中显示刚刚拍摄的照片,并添加了 2 个 UILabel(一个用于温度,一个用于位置)和一个 UIImageView(用于显示有关天气预报的图标).我直接在 StoryBoard 上绘制 UILabels 和 UIImageView。 现在我需要将图片与 2 个 UILabel 和 UIImageView 合并,有没有办法将它们合并到一个 UIImageView 中? 我必须这样做才能保存带有天气预报和位置的图片

更新

我创建了一个按钮来保存带有标签和 ImageView 的图片,我编写的代码是这样的:

- (IBAction)buttonSavePicture:(UIButton *)sender {
[self.imageView addSubview:self.labelPlace];
[self.imageView addSubview:self.labelTemperature];
[self.imageView addSubview:self.imageViewWeather];

UIGraphicsBeginImageContext(self.imageView.bounds.size);
[self.imageView.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *finalImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentationDirectory, NSUserDomainMask, YES);
NSString *filePath = [[paths objectAtIndex:0] stringByAppendingPathComponent:self.filename];

[UIImageJPEGRepresentation(finalImage, 1) writeToFile:filePath atomically:YES];

但是当我去Documents目录中查看我保存正确的图片时却找不到。

最佳答案

是的,您可以通过捕获它们轻松地做到这一点。按照步骤操作。

  1. 在 Storyboard中创建一个小的父 View ,将您想要捕获的所有控件放在一起。创建一个导出说 captureView

  2. 需要时调用以下函数。

    -(void)capture{
    
          UIGraphicsBeginImageContext(self.captureView.bounds.size);
          [self.captureView.layer renderInContext:UIGraphicsGetCurrentContext()];
          UIImage *capturedImage = UIGraphicsGetImageFromCurrentImageContext();
          UIGraphicsEndImageContext();
    
          //FINAL OUTPUT
          self.imageView.image=capturedImage;
    }
    

干杯。

关于ios - 在一个 uiview 中合并多个 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24526984/

相关文章:

ios - 在特定的缩放 iOS Swift 4 上更改 MKMapView 的 Pin 图像

ios - 将变量传递给 void ^() block

iOS:使用 AutoLayout 调整 UIImageView 的大小

ios - 如何在 UIImage 上显示标签?

iphone - 如何动态调整 scrollViewDidZoom 中的 contentInset?我最初在 UIScrollview 中设置了 UIImageView 的 contentInset

ios - CLLocationManager 在蜂窝网络 (3G) 中无法获取位置精度

ios - 核心数据 : Multiple xcdatamodeld files in the same iOS project?

ios - 分发应用程序时将文本文件放在哪里?

iphone - 如何在 Interface Builder 中旋转 UIImageView?

ios - UILabel 动态高度与 UIImageView 恒定高度