gpt4 book ai didi

iphone - 使用 UIImage imageWithData :NSData dataWithContentsOfFile 时未显示图像

转载 作者:行者123 更新时间:2023-11-28 22:53:26 24 4
gpt4 key购买 nike

下面的代码没有显示从网上下载并写入我的应用程序文档目录的图像。我查看了iphone的模拟器文档目录,下载的文件是存在的。但图像不显示。

当尝试使用 UIImage *imgBack = [UIImage imageNamed:@"btn-back.png"]; 添加资源图像时,uiimageview 显示图像。从中下载的图像在线并存储在应用程序目录中不显示图像。

请告诉我,谢谢

NSString *workSpacePath=[[self applicationDocumentsDirectory] stringByAppendingPathComponent:theFileName];
NSLog(@"%@ workSpacePath ",workSpacePath);
if ( workSpacePath ){

//--------------
UIImage *imgBack = [UIImage imageNamed:@"btn-back.png"];
imageView1 = [[[UIImageView alloc] initWithFrame:CGRectMake(0,0,20,20)] autorelease];
[imageView1 setBackgroundColor:[UIColor grayColor]];

imageView1.image = [UIImage imageWithData:[NSData dataWithContentsOfFile:workSpacePath]];
[cell addSubview:imageView1];
}

最佳答案

试试这个,这对我来说非常有效:

 - (void)viewDidLoad
{
  [super viewDidLoad];

NSURL *url1 = [NSURL URLWithString:@"http://mobiledevelopertips.com/images/logo-iphone-dev-tips.png"];
UIImage *image = [UIImage imageWithData: [NSData dataWithContentsOfURL:url1]];

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *savedImagePath = [documentsDirectory stringByAppendingPathComponent:@"savedImage.png"];

NSData *imageData = UIImagePNGRepresentation(image);
[imageData writeToFile:savedImagePath atomically:NO];

NSArray *paths1 = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory1 = [paths1 objectAtIndex:0];
NSString *workSpacePath = [documentsDirectory1 stringByAppendingPathComponent:@"savedImage.png"];
NSLog(@"%@ workSpacePath ",workSpacePath);

if ( workSpacePath )
{
imageView1 = [[UIImageView alloc] initWithFrame:CGRectMake(20,20,20,20)];
[imageView1 setBackgroundColor:[UIColor grayColor]];

imageView1.image = [UIImage imageWithData:[NSData dataWithContentsOfFile:workSpacePath]];
[self.view addSubview:imageView1];

}
}

关于iphone - 使用 UIImage imageWithData :NSData dataWithContentsOfFile 时未显示图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11322470/

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