gpt4 book ai didi

ios - 从文档目录为 UIbutton 设置图像

转载 作者:行者123 更新时间:2023-11-29 02:19:33 25 4
gpt4 key购买 nike

我尝试从文档目录为 UIButton 设置图像

我试过这个:

-(IBAction) vignettesBank1:(id)sender {

NSString *localPath = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];

NSString *imageBouton1 = [localPath stringByAppendingPathComponent:[[NSString alloc]initWithFormat:@"btn01.png"]];

[boutonVideo1 setImage:[UIImage imageNamed:imageBouton1] forState:UIControlStateNormal];

NSLog(@"imageBouton1=%@",imageBouton1);}

日志返回:imageBouton1=/var/mobile/Applications/164F9A40-10AF-402E-A46C-73084CAA8627/Documents/btn01.png

我也试试这个:

-(IBAction) vignettesBank1:(id)sender {

NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);

NSString *documentsDir = [documentPaths objectAtIndex:0];

NSString *pngfile = [documentsDir stringByAppendingPathComponent:@"btn01.png"];

[boutonVideo1 setImage:[UIImage imageNamed:pngfile] forState:UIControlStateNormal];

NSLog(@"imageBouton1=%@",pngfile);}

日志返回相同:imageBouton1=/var/mobile/Applications/164F9A40-10AF-402E-A46C-73084CAA8627/Documents/btn01.png

我的按钮没有加载我的图片谢谢你的帮助

最佳答案

imageNamed:用于您 bundle 中的文件。你应该使用

NSData *imgData = [[NSData alloc] initWithContentsOfURL:[NSURL fileURLWithPath: imageBouton1]];
[boutonVideo1 setImage:[[UIImage alloc] initWithData:pngfile] forState:UIControlStateNormal];

关于ios - 从文档目录为 UIbutton 设置图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28326332/

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