使用UIImage imageWithData时,图像未显示:NSData dataWithContentsOfFile [英] Image is not being shown when using UIImage imageWithData:NSData dataWithContentsOfFile

查看:105
本文介绍了使用UIImage imageWithData时,图像未显示:NSData dataWithContentsOfFile的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下代码不显示从在线下载并写入我的应用程序文档目录的图像。
我在iphone的模拟器文件目录里面检查过,下载的文件是存在的。
但不显示图像。

The below code is not displaying the image which is downloaded from online and written into my application documents directory. i have checked inside the iphone's simulator documents directory , the downloaded file is exist. but the image is not displayed.

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

When trying to add a resources image using UIImage *imgBack = [UIImage imageNamed:@"btn-back.png"]; , the uiimageview displays the image.bu the image which is downloaded from online and stored inside the app directory is not displaying the image.

请告诉我并谢谢

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];
}


推荐答案

试试这个,这是完美的为我工作:

try this ,this is perfectly working for me:

 - (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];

    }
}

这篇关于使用UIImage imageWithData时,图像未显示:NSData dataWithContentsOfFile的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆