如何将本地jpeg或png图像文件加载到iPhone应用程序中?

时间:2021-12-05 21:22:28

Or I should ask why is it so difficult for me? The answer is probably that I'm new to iPhone development and I'm trying to ditch my old methods and step into the new platform of the future. From start to finish, I have a few questions about this process...

或者我应该问为什么对我来说这么难?答案可能是,我对iPhone开发还不熟悉,我正在尝试抛弃旧的方法,进入未来的新平台。从头到尾,我对这个过程有几个问题……

  1. If I create a .png image, where does it go in my project's directory (i.e. my computer's hard drive)? Can I put it anywhere on my hard drive and Xcode will copy it in the right place when I load it into Xcode?
  2. 如果我创建了一个。png图像,它在我的项目目录(例如,我的计算机的硬盘驱动器)中的位置是什么?我可以把它放在硬盘上的任何地方吗?当我把它加载到Xcode时,Xcode会把它复制到正确的地方吗?
  3. Once it exists on my hard drive somewhere, how do I add it to my Xcode project? Can I just drag the file into any folder in the "Groups & Files" tree?
  4. 一旦它存在于我的硬盘上,我如何将它添加到我的Xcode项目中?我可以将文件拖放到“组和文件”树中的任何文件夹中吗?
  5. Once I drag it into Xcode, what copy settings do I want to use in that copy file dialog box that pops up? Copy items into destination group's folder checkbox? Reference Type? Recursively create groups for added folders?
  6. 一旦我将它拖到Xcode中,我想要在弹出的那个复制文件对话框中使用什么复制设置?将项目复制到目标组的文件夹复选框中?引用类型?递归地为添加的文件夹创建组?
  7. Once the .png image has properly been added to my project, what's the easiest way to get it into an CGImageRef? The documentation shows an example using the CGImageCreateWithPNGDataProvider helper method, but says it's not supported by the iOS SDK. I'd like to use the CGImageRef for repeatedly drawing the image to a bitmap context.
  8. 一旦.png映像被正确地添加到我的项目中,那么将它放入CGImageRef的最简单方法是什么?该文档显示了一个使用CGImageCreateWithPNGDataProvider helper方法的示例,但表示iOS SDK不支持该方法。我想使用CGImageRef将图像反复绘制到位图上下文。

Thanks so much in advance and I apologize for the lengthy question, I'm just surprised it's such a convoluted process compared to some other platforms.

非常感谢,我为这个冗长的问题道歉,我只是很惊讶这是一个如此复杂的过程相比于其他平台。

6 个解决方案

#1


17  

If you load into UIImage, you can get a CGImageReg:

如果你加载到UIImage,你可以得到一个CGImageReg:

    UIImage *image = [UIImage imageNamed:@"image.png"];
CGImageRef imageRef = [image CGImage];

#2


5  

1) It can go anywhere in Groups & Files, but if you only have a few, I'd recommend sticking it into the Resources groups. Remember, the groups in Groups & Files don't match actual folders in your directory structure however. They simply exist to organize your files.

1)它可以在组和文件中任意位置,但是如果只有几个,我建议将它放在资源组中。记住,组和文件中的组与目录结构中的实际文件夹不匹配。它们只是用来组织你的文件。

2) Just drag it into the Groups & Files pane.

2)将它拖到组和文件窗格中。

3) I'd use the 'copy' option. Not copying it is often useful when you're linking to shared resources used by different projects.

我会使用“复制”选项。当链接到不同项目使用的共享资源时,不复制它通常是有用的。

4) I haven't used CGImageRef myself, but if you just want a UIImage, you can simply call its imageNamed: class method and specify the image name without any additional paths leading up to it. It's assumed the file will be relative to your application's main bundle.

4)我自己还没有使用CGImageRef,但是如果您只想要一个UIImage,您可以简单地调用它的imageNamed: class方法并指定图像名称,而不需要任何其他路径指向它。假定该文件与应用程序的主包相关。

#3


3  

1 & 2) You can place it anywhere on your harddrive (so long as you don't move or delete it.. then you'll need to update the reference in XCode). Within XCode, you may want to create a "Group" for your images by right clicking in the left model tree and selecting it from the menu. Then just right click again and goto "Add" -> "Existing files or folders" and select the png file. If you accidentally add it in the wrong group, you can drag it around in XCode model tree. You can also drag and drop the file into a group in XCode from the Finder window.

1和2)你可以把它放在你的硬盘上的任何地方(只要你不移动或删除它)。然后需要更新XCode中的引用)。在XCode中,您可能想要为您的图像创建一个“组”,方法是在左边的模型树中右键单击并从菜单中选择它。然后再右键单击,然后“添加”->“现有文件或文件夹”,然后选择png文件。如果您不小心将它添加到错误的组中,您可以在XCode模型树中拖动它。您还可以从查找程序窗口将文件拖放到XCode中的组中。

3) You can just use "Default" copy settings and you don't need to copy it into the project. A reference is good enough and no need to recursively create groups.

3)您可以使用“默认”复制设置,您不需要将其复制到项目中。引用足够好,不需要递归地创建组。

4) What are you trying to do with the image? Do you need a CGImageRef from the png file or could you use an UIImageView or UIImage to load it from the file, or you can just lay it out in Interface Builder (IB). If you elaborate a little bit more about how you are using the image, maybe we can help you.

4)你想用这张照片做什么?你需要从png文件中获取CGImageRef,还是使用UIImageView或UIImage从文件中加载它,或者你可以在Interface Builder (IB)中显示出来。如果你再详细说明一下你是如何使用图像的,也许我们可以帮助你。

#4


2  

If you have a particular image or set of images that you want included as part of the application, you can simply drag them into the project -- I prefer the Resources section for this -- and reference them by name using UIImage "imageNamed" for initialization.

如果你有一个特定的图片或一组你想要包含在应用程序中的图片,你可以简单地将它们拖到项目中——我更喜欢参考资料部分——并使用UIImage "imageNamed"来引用它们进行初始化。

If you are trying to load images that are local to an individual iPhone, that uses different techniques, but that doesn't seem to be what you are after.

如果你想加载一个iPhone本地的图片,使用不同的技术,但这似乎不是你想要的。

I'm new to iPhone programming too, so I hope this helps.

我也是iPhone编程新手,所以我希望这能有所帮助。

#5


2  

place image in the bundle then run this function

将图像放在包中,然后运行这个函数

NSString *localPath = [[NSBundle mainBundle]bundlePath];

NSString *imageName = [localPath stringByAppendingPathComponent:[[NSString alloc]initWithFormat:@"worldMapResized.png"]];

CGRect bgRect = CGRectMake(0, 0, 500, 500);

UIImageView *myBgView = [[UIImageView alloc] initWithFrame:bgRect];

[myBgView setImage:[UIImage imageWithContentsOfFile:imageName]];

[self.view addSubview:myBgView];

#6


0  

USING XCODE:

使用XCODE:

if you have a UIImage object (here named buttonScan) positioned in your XIB file and referrenced in your .h file, in viewDidLoad of your .m file, you can use this code to reference an image file that you called buttonScan01a.png (which you imported into your project using the FILE > ADD FILES TO ...

如果在您的XIB文件中有一个UIImage对象(这里命名为buttonScan),并在.h文件中引用,在.m文件的viewDidLoad中,您可以使用此代码引用一个名为buttonScan01a的图像文件。png(使用>文件将文件添加到…

buttonScan.image = [UIImage imageNamed:@"buttonScan01a.png"];
[buttonScan sizeToFit];

#1


17  

If you load into UIImage, you can get a CGImageReg:

如果你加载到UIImage,你可以得到一个CGImageReg:

    UIImage *image = [UIImage imageNamed:@"image.png"];
CGImageRef imageRef = [image CGImage];

#2


5  

1) It can go anywhere in Groups & Files, but if you only have a few, I'd recommend sticking it into the Resources groups. Remember, the groups in Groups & Files don't match actual folders in your directory structure however. They simply exist to organize your files.

1)它可以在组和文件中任意位置,但是如果只有几个,我建议将它放在资源组中。记住,组和文件中的组与目录结构中的实际文件夹不匹配。它们只是用来组织你的文件。

2) Just drag it into the Groups & Files pane.

2)将它拖到组和文件窗格中。

3) I'd use the 'copy' option. Not copying it is often useful when you're linking to shared resources used by different projects.

我会使用“复制”选项。当链接到不同项目使用的共享资源时,不复制它通常是有用的。

4) I haven't used CGImageRef myself, but if you just want a UIImage, you can simply call its imageNamed: class method and specify the image name without any additional paths leading up to it. It's assumed the file will be relative to your application's main bundle.

4)我自己还没有使用CGImageRef,但是如果您只想要一个UIImage,您可以简单地调用它的imageNamed: class方法并指定图像名称,而不需要任何其他路径指向它。假定该文件与应用程序的主包相关。

#3


3  

1 & 2) You can place it anywhere on your harddrive (so long as you don't move or delete it.. then you'll need to update the reference in XCode). Within XCode, you may want to create a "Group" for your images by right clicking in the left model tree and selecting it from the menu. Then just right click again and goto "Add" -> "Existing files or folders" and select the png file. If you accidentally add it in the wrong group, you can drag it around in XCode model tree. You can also drag and drop the file into a group in XCode from the Finder window.

1和2)你可以把它放在你的硬盘上的任何地方(只要你不移动或删除它)。然后需要更新XCode中的引用)。在XCode中,您可能想要为您的图像创建一个“组”,方法是在左边的模型树中右键单击并从菜单中选择它。然后再右键单击,然后“添加”->“现有文件或文件夹”,然后选择png文件。如果您不小心将它添加到错误的组中,您可以在XCode模型树中拖动它。您还可以从查找程序窗口将文件拖放到XCode中的组中。

3) You can just use "Default" copy settings and you don't need to copy it into the project. A reference is good enough and no need to recursively create groups.

3)您可以使用“默认”复制设置,您不需要将其复制到项目中。引用足够好,不需要递归地创建组。

4) What are you trying to do with the image? Do you need a CGImageRef from the png file or could you use an UIImageView or UIImage to load it from the file, or you can just lay it out in Interface Builder (IB). If you elaborate a little bit more about how you are using the image, maybe we can help you.

4)你想用这张照片做什么?你需要从png文件中获取CGImageRef,还是使用UIImageView或UIImage从文件中加载它,或者你可以在Interface Builder (IB)中显示出来。如果你再详细说明一下你是如何使用图像的,也许我们可以帮助你。

#4


2  

If you have a particular image or set of images that you want included as part of the application, you can simply drag them into the project -- I prefer the Resources section for this -- and reference them by name using UIImage "imageNamed" for initialization.

如果你有一个特定的图片或一组你想要包含在应用程序中的图片,你可以简单地将它们拖到项目中——我更喜欢参考资料部分——并使用UIImage "imageNamed"来引用它们进行初始化。

If you are trying to load images that are local to an individual iPhone, that uses different techniques, but that doesn't seem to be what you are after.

如果你想加载一个iPhone本地的图片,使用不同的技术,但这似乎不是你想要的。

I'm new to iPhone programming too, so I hope this helps.

我也是iPhone编程新手,所以我希望这能有所帮助。

#5


2  

place image in the bundle then run this function

将图像放在包中,然后运行这个函数

NSString *localPath = [[NSBundle mainBundle]bundlePath];

NSString *imageName = [localPath stringByAppendingPathComponent:[[NSString alloc]initWithFormat:@"worldMapResized.png"]];

CGRect bgRect = CGRectMake(0, 0, 500, 500);

UIImageView *myBgView = [[UIImageView alloc] initWithFrame:bgRect];

[myBgView setImage:[UIImage imageWithContentsOfFile:imageName]];

[self.view addSubview:myBgView];

#6


0  

USING XCODE:

使用XCODE:

if you have a UIImage object (here named buttonScan) positioned in your XIB file and referrenced in your .h file, in viewDidLoad of your .m file, you can use this code to reference an image file that you called buttonScan01a.png (which you imported into your project using the FILE > ADD FILES TO ...

如果在您的XIB文件中有一个UIImage对象(这里命名为buttonScan),并在.h文件中引用,在.m文件的viewDidLoad中,您可以使用此代码引用一个名为buttonScan01a的图像文件。png(使用>文件将文件添加到…

buttonScan.image = [UIImage imageNamed:@"buttonScan01a.png"];
[buttonScan sizeToFit];