Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to display image on UIImageView using URL and NSString in ios?

I want to display image on UIImageView using URL and NSString. I am unable to show image.

My code is:

UIImageView *view_Image = [[UIImageView alloc]initWithFrame:CGRectMake(view_Image_Pos_X, view_Image_Pos_Y, 179, 245)];

view_Image.backgroundColor = [UIColor greenColor];
view_Image.tag = img;
view_Image.userInteractionEnabled=YES;
view_Image.autoresizesSubviews = YES;
view_Image.alpha = 0.93;  
[self.view addSubview:view_Image];               

Here what i am trying:

 if (img == 0) {


 NSString *url_Img1 = @"http://opensum.in/app_test_f1/;";
 NSString *url_Img2 = @"45djx96.jpg";

 NSString *url_Img_FULL = [NSString stringWithFormat:@"%@%@", url_Img1,url_Img2];

 NSLog(@"Show url_Img_FULL: %@",url_Img_FULL);



 NSURL *url_img = [NSURL URLWithString:url_Img_FULL];
 NSLog(@"Show: url_img %@",url_img);

// Here below line working perfectly and image is showing             

view_Image.image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:@"http://opensum.in/app_test_f1/45djx96.jpg"]]];   // working code

but i dont want this i want to concatanate two url make one url(ie;url_Img_FULL) and then pass to an image like below:

UIImage *image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:url_Img_FULL]]]; // Not Working

I Want from "url_Img_FULL" (ie: combination of two url) The image will show. You can check also the url is working properly. Any idea?

like image 977
Areeba Khan Avatar asked Nov 23 '12 11:11

Areeba Khan


People also ask

What is a UIImageView object?

An object that displays a single image or a sequence of animated images in your interface. Image views let you efficiently draw any image that can be specified using a UIImage object. For example, you can use the UIImageView class to display the contents of many standard image files, such as JPEG and PNG files.

How to use the iOS UI image view in iOS applications?

We can use Image View in our iOS applications by adding UIImageView class reference. Now we will see how to use the iOS UI image view in our iOS applications with example. To create a new project in iOS Xcode open Xcode from /Applications folder directory. Once we open Xcode the welcome window will open like as shown below.

How to create a new UIImageView programmatically in Swift?

To create a new UIImageView programmatically in Swift, we need to create a new instance of UIImageView class and then set UIImage to an image property. Like so: The code snippet above creates a new instance of UIImageView and adds an image to it. However, it is not enough to make an image display.

Why does the background of an image show up in uiimage?

Any transparency in the image allows the image view’s background to show through. Similarly, any further transparency in the background of the image is dependent on the transparency of the image view and the transparency of the UIImage object it displays.


2 Answers

    NSString *url_Img1 = @"http://opensum.in/app_test_f1";
    NSString *url_Img2 = @"45djx96.jpg";

    NSString *url_Img_FULL = [url_Img1 stringByAppendingPathComponent:url_Img2];

    NSLog(@"Show url_Img_FULL: %@",url_Img_FULL);
    view_Image.image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:url_Img_FULL]]]; 

try this.

like image 81
Nookaraju Avatar answered Oct 07 '22 01:10

Nookaraju


Just do it

NSString *imgURL = @"imagUrl";

NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:imgURL]];

[YourImgView setImage:[UIImage imageWithData:data]];

Using GCD : If you don't want to hang your application then you can download your image in background.

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        NSString *imgURL = @"imagUrl";
        NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:imgURL]];

    //set your image on main thread.
    dispatch_async(dispatch_get_main_queue(), ^{
        [YourImgView setImage:[UIImage imageWithData:data]];
    });    
});
like image 45
Rajneesh071 Avatar answered Oct 06 '22 23:10

Rajneesh071