xcode 如何将本地 jpeg 或 png 图像文件加载到 iPhone 应用程序中?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3103840/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do you load a local jpeg or png image file into an iPhone app?
提问by BeachRunnerFred
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 开发不熟悉,我正在尝试抛弃我的旧方法并进入未来的新平台。从头到尾,我对这个过程有几个疑问……
- 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?
- 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?
- 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?
- 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.
- 如果我创建了一个 .png 图像,它在我的项目目录(即我的计算机的硬盘驱动器)中的哪个位置?我可以将它放在硬盘上的任何位置,当我将它加载到 Xcode 中时,Xcode 会复制它到正确的位置吗?
- 一旦它存在于我的硬盘驱动器上,我该如何将它添加到我的 Xcode 项目中?我可以将文件拖到“组和文件”树中的任何文件夹中吗?
- 将它拖入 Xcode 后,我想在弹出的复制文件对话框中使用哪些复制设置?将项目复制到目标组的文件夹复选框?参考类型?为添加的文件夹递归创建组?
- 将 .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.
非常感谢,对于这个冗长的问题,我深表歉意,与其他一些平台相比,这是一个如此复杂的过程,我感到很惊讶。
回答by Cheddar
If you load into UIImage, you can get a CGImageReg:
如果你加载到 UIImage 中,你可以得到一个 CGImageReg:
UIImage *image = [UIImage imageNamed:@"image.png"];
CGImageRef imageRef = [image CGImage];
回答by Joost Schuur
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) 它可以放在 Groups & Files 中的任何地方,但如果你只有几个,我建议把它放在 Resources 组中。请记住,组和文件中的组与目录结构中的实际文件夹不匹配。它们只是为了组织您的文件而存在。
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.
3)我会使用“复制”选项。当您链接到不同项目使用的共享资源时,不复制它通常很有用。
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:类方法并指定图像名称,而不需要任何额外的路径。假设该文件将与您的应用程序的主包相关。
回答by iwasrobbed
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 模型树中拖动它。您还可以从 Finder 窗口将文件拖放到 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)你想用图像做什么?您是否需要CGImageRef
png 文件中的 ,或者您可以使用UIImageView
或UIImage
从文件中加载它,或者您可以将它放在Interface Builder(IB)中。如果您详细说明您如何使用图像,也许我们可以帮助您。
回答by Silverlock
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 编程的新手,所以我希望这会有所帮助。
回答by r farnell
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];
回答by tony gil
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 ...
如果您有一个 UIImage 对象(此处命名为buttonScan
)位于您的 XIB 文件中并在您的 .h 文件中引用,则在 .m 文件的 viewDidLoad 中,您可以使用此代码来引用您调用的图像文件buttonScan01a.png
(您导入到您的项目使用 FILE > ADD FILES TO ...
buttonScan.image = [UIImage imageNamed:@"buttonScan01a.png"];
[buttonScan sizeToFit];