在我的iPad应用程序中,我试图以编程方式加载图像文件.该文件不是我的项目的一部分,因此它不在XCode中引用.XCode的"组和文件"列中没有该文件的条目.
图像必须在运行时加载,它的名称是从属性列表中读取的.
我正在尝试加载这样的文件:
NSString* pathToImageFile = [[NSBundle mainBundle] pathForResource:@"MyImage" ofType:@"png" inDirectory:@"MyDirectory"];
UIImage* retVal = [UIImage imageWithContentsOfFile:pathToImageFile];
Run Code Online (Sandbox Code Playgroud)
在这种情况下,mydirectory生活在主要包中,如下所示:
MyAmazingApp.app/MyDirectory/MyImage.png
Run Code Online (Sandbox Code Playgroud)
不幸的是,图像无法加载.我无法将图像文件添加到我的项目中,因为它的名称将在运行时确定,并且无法提前知道.在运行时从配置文件(属性列表)中读取文件名.
我究竟做错了什么?非常感谢您的帮助.
我写了一个应该在iPad和iPhone上运行的应用程序.我正在使用AVAudioPlayer播放声音.现在我遇到了音量水平的一些问题.
在iPad上运行时,一切都很好,播放声音的音量水平也很好,在iPad模拟器中运行也是如此.
当应用程序在iPhone上运行时出现问题:虽然iPhone模拟器中的音量水平很好,但设备上的音量非常低.
这是我在两台设备上使用的代码:
if (audioPlayerAtmo==nil)
{
NSString *filename = [NSString stringWithFormat:@"Atmo_%i", currentPage];
NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:filename ofType:@"mp3"]];
AVAudioPlayer *tempPlayer =[[AVAudioPlayer alloc] initWithContentsOfURL:url error:NULL];
tempPlayer.delegate = self;
//NSLog(@"tempPlayer.volume: %f", [tempPlayer volume]);
[tempPlayer setVolume:1.0f];
//NSLog(@"tempPlayer.volume: %f", [tempPlayer volume]);
self.audioPlayerAtmo = tempPlayer;
[tempPlayer release];
[audioPlayerAtmo play];
btAtmo.selected = YES;
}
else // player exists
{
// ...
}
Run Code Online (Sandbox Code Playgroud)
有人知道为什么iPhone的级别如此之低,而模拟器和iPad上的一切都很好吗?
在此先感谢您的帮助.
技术数据:XCode 3.2.4 iPhone 4(Vers.4.1)