简体   繁体   English

在 UIImageView 上获取图像

[英]Getting image on UIImageView

I'm currently making an app where the user takes a photo or select it from an album, then an overlay is placed over the image.我目前正在制作一个应用程序,用户在其中拍摄照片或 select 从相册中拍摄,然后在图像上放置一个叠加层。 The user can then scale, rotate and save the image.然后用户可以缩放、旋转和保存图像。

For now, I have 2 xib files.现在,我有 2 个 xib 文件。 The first one has thumbnails for overlay pictures to choose from.第一个具有可供选择的叠加图片的缩略图。 Clicking on one of them goes to another xib file where I have 2 UIImageViews.单击其中一个会转到另一个 xib 文件,其中我有 2 个 UIImageViews。 One for the overlay and one for the camera.一个用于叠加层,一个用于相机。 Previously I just used interface builder to set an image at the attributes.以前我只是使用界面生成器在属性处设置图像。 But if have 20 overlays, it means I have to use 20 xibs.但是如果有 20 个覆盖,这意味着我必须使用 20 个 xib。 So I thought the best way is to add pictures through code so I can use this 2nd xib as a template for the overlays.所以我认为最好的方法是通过代码添加图片,这样我就可以使用这个 2nd xib 作为覆盖的模板。 Here's when I'm stuck.这是我被卡住的时候。 When I click on a thumbnail in the 1st xib file, how do tell the 2nd xib file to store an overlay in the UIImageView?当我单击第一个 xib 文件中的缩略图时,如何告诉第二个 xib 文件在 UIImageView 中存储覆盖?

For the camera, I'm using对于相机,我正在使用

imageView.image = [info objectForKey:@"UIImagePickerControllerOriginalImage"];

and it works fine.它工作正常。 I can even save it to the album.我什至可以将它保存到相册中。 I thought I could use the code below for the overlays but that didn't work.我以为我可以将下面的代码用于叠加层,但这不起作用。

UIImage *image = [UIImage imageNamed: @"char1.png"];

Hope someone can help.希望有人可以提供帮助。 Thanks very much.非常感谢。

Hakimo哈基莫

You don't need to use xib at all in this situation.在这种情况下,您根本不需要使用 xib。 Create UIImageView by hands, set its frame and image, add it as a subview to your viewControllers.view.手动创建 UIImageView,设置其框架和图像,将其作为子视图添加到您的 viewControllers.view。 You can have as many UIImageViews as you need.您可以根据需要拥有任意数量的 UIImageView。 Make an array of them.制作一个数组。 Your case is obviously misuse of xibs concept.您的案例显然是滥用 xibs 概念。 Xibs are usually used when there is need to create some complicated view with many subviews that wouldn't be used in code, like labels, decoration images etc. Xibs 通常用于需要创建一些复杂的视图,其中包含许多不会在代码中使用的子视图,如标签、装饰图像等。

CGRect frameForImage = CGRectMake(100, 100, 200, 300);
UIImageView *originalImage = [[UIImageView alloc] initWithFrame:frameForImage];
originalImage.image = [info objectForKey:@"UIImagePickerControllerOriginalImage"];
[self.view addSubview:originalImage];
[originalImage release];

frameForImage.origin.x += 400;
UIImageView *resizedImage = [[UIImageView alloc] initWithFrame:frameForImage];
resizedImage.image = [info objectForKey:@"UIImagePickerControllerWhateverImage"];
[self.view addSubview:resizedImage];
[resizedImage release];

I think you can have two controllers.As you said first one will be static.我想你可以有两个控制器。正如你所说的第一个是 static。 You can declare the second controller as a class with outlets and ivars.您可以将第二个 controller 声明为带有 outlets 和 ivars 的 class。 Then you can create instances of that class whenever you you click either of the two controls on the first controller with the appropriate IMageviews (or Images).This is just the concept.然后,只要您使用适当的 IMageviews(或图像)单击第一个 controller 上的两个控件中的任何一个,您就可以创建该 class 的实例。这只是概念。 I assume you can code this easily.我假设您可以轻松编写代码。

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

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