1

I have one Url :http://192.168.3.178:8090/SaveDollar/rest/deals/dealimages I am trying Get the Image from Url.

What I tried:

-(void)connectionDidFinishLoading:(NSURLConnection *)connection
{
    NSMutableArray *al=[NSJSONSerialization JSONObjectWithData:webData options:0 error:nil];
        NSLog(@"all NSMutable2");

   for (NSDictionary *diction in al)
    {
    Name.text=[diction valueForKey:@"dealImage"];
           NSString* str = Name.text;
           NSLog(@"%@dataImage is ",str);
           NSData* data = [str dataUsingEncoding:NSUTF8StringEncoding];
           NSUInteger len = data.length;
           uint8_t *bytes = (uint8_t *)[data bytes];
           NSMutableString *result = [NSMutableString stringWithCapacity:len * 3];
           [result appendString:@"["];
           for (NSUInteger i = 0; i < len; i++)
            {
                if (i)
                {   
                    [result appendString:@","];
                }
                [result appendFormat:@"%d", bytes[i]];
            }

            [result appendString:@"]"];
            NSLog(@"result%@",result);
            NSLog(@"uint8_t%s",bytes);
            NSMutableData *newData = [NSMutableData new];
            NSScanner *theScanner = [NSScanner scannerWithString: result];
             theScanner.charactersToBeSkipped = [NSCharacterSet characterSetWithCharactersInString:@"[],"];
            while ([theScanner isAtEnd] == NO) {
                int a;
                [theScanner scanInt:&a];
                uint8_t b = a;
                [newData appendBytes:(const void *)&b length:1];
            }
            UIImage *newImage = [UIImage imageWithData:newData];

            UIImageView *imageView=[[UIImageView alloc] initWithImage:newImage];
            imageView.frame = CGRectMake(0, 25 , 400,150);

            [self.view addSubview:imageView];
}
}

First I am converted String to NSdata and then NSData converted Byte data Second I am Converted Byte data to NSdata and then NSdata is pass to the UIImage

My Problem : Image not Displaying but Database have image so Please Give me any idea about my problem

JSON FormatViewer like this:

JSON
0
dealImageId : 1
dealImage : "WjQyNDAzMTM2Mzk2NTMxMzE="
dealId : 2
createdDate : 1393871400000
modifiedDate : 1399314600000
createdUserId : 2
modifiedUserId : 4
dealPosted
deleted : true
1
dealImageId : 3
dealImage : "5mQ4ZmZlMTdkY2E0NTc4Njk2NjAwMDA0OTQ5MmEwMDA4MDAwMDAwMGUwMDBlMDEwMjAwMWUwMDAwMDBiNjAwMDAwMDBmMDEwMjAwMTAwMDAwMDBkNDAwMDAwMDEwMDEwMjAwMGEwMDAwMDBlNDAwMDAwMDEyMDEwMzAwMDEwMDAwMDAwMTAwMDAwMDIwMjgyZTJlMmUyOQ=="
dealId : 1
createdDate : 1391365800000
modifiedDate : 1391365800000
createdUserId : 1
modifiedUserId : 2
dealPosted
deleted : false
7
  • Show example result from your web service. Log webData and al. Commented Apr 4, 2014 at 7:19
  • please add some log out, at least part of image data Commented Apr 4, 2014 at 7:19
  • what do you get back from that URL? is it a jpeg? See the imageWithData: method of UIImage class, which may be relevant. Commented Apr 4, 2014 at 7:22
  • @Wain Thanks for Reply but i trying get image on Url when i am Hit url it's showing String Format so I am Trying NSString to NSdata Conversion and Then NSdata Converted Byte data and Then Byte Data conversion to NSData finally NSdata Pass to UIImageView Commented Apr 4, 2014 at 7:26
  • I understand what you're doing, it doesn't change the need to see what data is actually returned... Commented Apr 4, 2014 at 7:30

4 Answers 4

2

The problem is that your URL is wrong. A copy and paste of the url into your browser should display the image. Once you have a valid Image URL you can display it easily with this method:

NSURL *url=[NSURL URLWithString:[NSString stringwithFormat:@"VALID IMAGE URL"]];
UIImage *img = [UIImage imageWithData:[NSData dataWithContentsOfURL:url]];

For instance, try your code with a different image url.

Sign up to request clarification or add additional context in comments.

2 Comments

Thank for Reply my image Format like this : dealImage : "WjQyNDAzMTM2Mzk2NTMxMzE="
there is no image format like that, what is the complete url you are trying to get an image from? Post an URL that, as explained on my answer, will display an image when copied directly into the web browser. Or are you trying to load a locally stored image? Like in the same network? Because the address you posted starts with 192.168.3.178 which means is stored on your network. If you are trying to get it from a database hosted by you, you would need the PUBLIC address to retrieve it. Also, the address you posted ends with a directory path, not a file.
2

Try this.

yourimg.image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:@"Your Image URL"]]];

2 Comments

@indainarmy Thank for Reply my image Format like this : dealImage : "WjQyNDAzMTM2Mzk2NTMxMzE="
@user3488098 whatever format is , you have to take particular url and put it in above code.
2

To display an image from a base64 string you should try like this:

NSURL *URL = [NSURL URLWithString:
[NSString stringWithFormat:@"data:application/octet-stream;base64,%@", yourBase64String]];

dealImage.image = [UIImage imageWithData:[NSData dataWithContentsOfURL:URL]];

EDIT:

If you need more on this have a look at the below links:

base64-decoding-in-ios-7

how-to-decode-base64-encoded-png-using-objective-c

Comments

0

Try setImageWithURL: function in AFNetworking. Url specified should be a direct link to image.

[YourImageView setImageWithURL:@"Url"];

Url specified should be a direct link to image.

1 Comment

Thank for Reply my image Format like this : dealImage : "WjQyNDAzMTM2Mzk2NTMxMzE=" –

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.