gpt4 book ai didi

ios - 无法以编程方式将文件从包加载到 UITextView 和 UIImageView

转载 作者:行者123 更新时间:2023-11-29 03:27:36 25 4
gpt4 key购买 nike

我一直在尝试将主包中的图像和文本文件加载到 UITextView 和 UIImageView 框中。我已经从 .xib 文件正确连接了 socket ,并确保我尝试加载的文件存在于 bundle 中,但它们没有加载,当我通过 NSLog 测试它们时,它们返回为 null。下面复制的是 .h 和 .m 文件中的代码。如果您想知道,有些 channel 很强大,因为我一直在尝试弄乱设置以使其正常工作,但到目前为止无济于事。

“.h文件”

@interface CluesViewController : UIViewController

@property (strong, nonatomic) IBOutlet UIImageView *cluesBackground;
@property (weak, nonatomic) IBOutlet UIImageView *titleImage;
@property (weak, nonatomic) IBOutlet UIImageView *typeATitle;
@property (weak, nonatomic) IBOutlet UIImageView *typeBTitle;
@property (weak, nonatomic) IBOutlet UIImageView *divider;
@property (weak, nonatomic) IBOutlet UITextView *typeAText;
@property (strong, nonatomic) IBOutlet UITextView *typeBText;

-(void)updateCluesViewInfoTypeA:(NSString *)typeA updateCluesViewInfoTypeB:(NSString *)typeB;

@end

“.m文件”

-(void)updateCluesViewInfoTypeA:(NSString *)typeA updateCluesViewInfoTypeB:(NSString *)typeB {

NSString *bGImageString = NULL;
bGImageString = [NSString stringWithFormat:@"%@&%@-bg", typeA, typeB];
NSString *bGPath = [[NSBundle mainBundle] pathForResource:bGImageString ofType:@"png"];
UIImage *bGImage = [UIImage imageNamed:bGPath];
[_cluesBackground setImage:bGImage];

NSString *titleImageString = NULL;
titleImageString = [NSString stringWithFormat:@"%@&%@-clues", typeA, typeB];
NSString *titleImagePath = [[NSBundle mainBundle] pathForResource:titleImageString ofType:@"png"];
UIImage *titleImage = [UIImage imageNamed:titleImagePath];
[_titleImage setImage:titleImage];

NSString *typeATitleImageString = NULL;
typeATitleImageString = [NSString stringWithFormat:@"%@-text", typeA];
NSString *typeATitlePath = [[NSBundle mainBundle] pathForResource:typeATitleImageString ofType:@"png"];
UIImage *typeATitleImage = [UIImage imageNamed:typeATitlePath];
[_typeATitle setImage:typeATitleImage];

NSString *typeBTitleImageString = NULL;
typeBTitleImageString = [NSString stringWithFormat:@"%@-text", typeB];
NSString *typeBTitlePath = [[NSBundle mainBundle] pathForResource:typeBTitleImageString ofType:@"png"];
UIImage *typeBTitleImage = [UIImage imageNamed:typeBTitlePath];
[_typeBTitle setImage:typeBTitleImage];

NSString *dividerImageString = NULL;
dividerImageString = [NSString stringWithFormat:@"%@&%@-divider", typeA, typeB];
NSString *dividerImagePath = [[NSBundle mainBundle] pathForResource:dividerImageString ofType:@"png"];
UIImage *dividerImage = [UIImage imageNamed:dividerImagePath];
[_divider setImage:dividerImage];

NSString *typeATextString = NULL;
typeATextString = [NSString stringWithFormat:@"%@Clues", typeA];
NSString *typeATextPath = [[NSBundle mainBundle] pathForResource:typeATextString ofType:@"txt"];
NSString* typeATextContent = [NSString stringWithContentsOfFile:typeATextPath encoding:NSASCIIStringEncoding error:NULL];
self.typeAText.text = typeATextContent;

NSString *typeBTextString = NULL;
typeBTextString = [NSString stringWithFormat:@"%@Clues", typeB];
NSString *typeBTextPath = [[NSBundle mainBundle] pathForResource:typeBTextString ofType:@"txt"];
NSString* typeBTextContent = [NSString stringWithContentsOfFile:typeBTextPath encoding:NSASCIIStringEncoding error:NULL];
self.typeBText.text = typeBTextContent;

NSLog(@"The textfield is %@", self.typeBText.text);
}

最佳答案

您使用的imageNamed:不正确。您应该只传递图像名称:

NSString *bGImageString = [NSString stringWithFormat:@"%@&%@-bg", typeA, typeB];
UIImage *bGImage = [UIImage imageNamed:bGImageString];

对其他图像也进行此更改。您只想将图像的完整路径与 UIImage imageWithContentsOfFile: 一起使用,而不是 imageNamed:

对于文件,您确定文件编码是 ASCII 而不是 UTF-8 吗?

如果编码不是问题,则使用 error 参数查看问题所在。

NSString *typeATextString = [NSString stringWithFormat:@"%@Clues", typeA];
NSString *typeATextPath = [[NSBundle mainBundle] pathForResource:typeATextString ofType:@"txt"];
NSError *error = nil;
NSString* typeATextContent = [NSString stringWithContentsOfFile:typeATextPath encoding:NSASCIIStringEncoding error:&error];
if (typeATextContent) {
self.typeAText.text = typeATextContent;
} else {
NSLog(@"Unable to load text from %@: %@", typeATextPath, error);
}

关于ios - 无法以编程方式将文件从包加载到 UITextView 和 UIImageView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20259047/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com