2

Trying to display the image downloaded from web service to UIImage, could get the exact image path( by URLString) and is displayed in NSLog. Now the problem is imageData is returning null value instead of the value to be returned.

The error message is to be printed as :

The operation couldn’t be completed. (Cocoa error 256.)

code used for retrieving the image path are described below,

    NSString *urlString = [result objectForKey:@"image"];
    NSLog(@"url image is %@ \n",urlString);

    NSData *imageData = [[NSData alloc] init];

    NSError* error = nil; 
    imageData = [NSData dataWithContentsOfURL:[NSURL URLWithString:urlString] options:NSDataReadingUncached error:&error];

    if (error) 
    {
        NSLog(@"%@", [error localizedDescription]);
    } 
    else
    {
        NSLog(@"image data is %@ \n",imageData);   
        imageview.image = [UIImage imageWithData:imageData];
    }

What is to be done, for displaying the image in the UIImage.

4

2 に答える 2

1

この部分は間違っています:

if (error) 
{
    NSLog(@"%@", [error localizedDescription]);
}

else
NSLog(@"image data is %@ \n",imageData);   
imageview.image = [UIImage imageWithData:imageData];

そのはず:

if (error) 
{
    NSLog(@"%@", [error localizedDescription]);
}

else
{
    imageview.image = [UIImage imageWithData:imageData];
}

また、NSData を割り当ててから、値を別の値で上書きします。この行は使用しないでください。

NSData *imageData = [[NSData alloc] init];

オブジェクトを割り当て/初期化してから破棄するのは、時間とメモリの無駄です。

于 2013-09-18T10:29:23.510 に答える