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
BenMorel
  • 34,448
  • 50
  • 182
  • 322
  • Show example result from your web service. Log `webData` and `al`. – Wain Apr 04 '14 at 07:19
  • please add some log out, at least part of image data – sage444 Apr 04 '14 at 07:19
  • what do you get back from that URL? is it a jpeg? See the imageWithData: method of UIImage class, which may be relevant. – RobP Apr 04 '14 at 07: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 –  Apr 04 '14 at 07:26
  • I understand what you're doing, it doesn't change the need to see what data is actually returned... – Wain Apr 04 '14 at 07:30
  • @Wain but how can i Retried Image on Url Give me any Idea why image is not Retrieved in my code please help me –  Apr 04 '14 at 07:36
  • @user3488098 dealImages is it a zipfile having images?? .In that case you have to unzip and then load images. – Dileep Apr 04 '14 at 07:45

4 Answers4

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.

Pochi
  • 13,391
  • 3
  • 64
  • 104
  • Thank for Reply my image Format like this : dealImage : "WjQyNDAzMTM2Mzk2NTMxMzE=" –  Apr 04 '14 at 09:54
  • 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. – Pochi Apr 04 '14 at 10:58
2

Try this.

yourimg.image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:@"Your Image URL"]]];
Darshan Kunjadiya
  • 3,323
  • 1
  • 29
  • 31
Rushi trivedi
  • 737
  • 1
  • 15
  • 37
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

Community
  • 1
  • 1
NeverHopeless
  • 11,077
  • 4
  • 35
  • 56
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.

Dileep
  • 2,399
  • 4
  • 26
  • 39
  • Thank for Reply my image Format like this : dealImage : "WjQyNDAzMTM2Mzk2NTMxMzE=" – –  Apr 04 '14 at 09:55