0

I am developing in Objective-C , and I want to show the image via http url in AlertView ?

I already get the image url like:http://192.72.1.1/DCIM/100__DSC/SNAP0053.JPG , and convert the NSURL to NSString. But I can not show the image in AlertView , it is empty.

The code for convert the url to NSString and show in AlertView is like the following:

NSString *path = [url absoluteString];
NSData * imageData = [[NSData alloc] initWithContentsOfURL: [NSURL URLWithString: path]];
UIImage *Image=[UIImage imageWithData:imageData];

UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Image" message:nil delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];

UIImageView *imageView = [[UIImageView alloc] init];

[imageView setImage:Image];

[alert addSubview:imageView];
[alert show];

But the AlertView did not show any image like the following picture. enter image description here

Did I missing something ?

How to show the image via http url in AlertView in Objective-C ?

Wun
  • 6,211
  • 11
  • 56
  • 101
  • You can add subview to `UIAlertView` since iOS7 (If my memory doesn't fail me). You have to use a custom `UIAlertView` (many project on GitHub that do the same behaviour) – Larme Feb 10 '15 at 08:14
  • have tried this http://stackoverflow.com/a/2323694? I think you not set its x,y,width and height property – Mrugesh Tank Feb 10 '15 at 08:16

2 Answers2

1

You cant add image in AlertView. It was possible prior to iOS 7. You can create a custom AlertView and add image to it.

Also check out this

https://github.com/wimagguc/ios-custom-alertview

yoshiiiiiiii
  • 953
  • 8
  • 20
0

No, you can't do that. Write a custom control with UIView which imitate the behavior of UIAlertView.

Some suggestions:

  1. Never use addSubView on UIAlertView.
  2. UIAlertView is deprecated use UIAlertController.

Reference UIAlertView Class Reference:

1)

The UIAlertView class is intended to be used as-is and does not support subclassing. The view hierarchy for this class is private and must not be modified.

2)

Important: UIAlertView is deprecated in iOS 8. (Note that UIAlertViewDelegate is also deprecated.) To create and manage alerts in iOS 8 and later, instead use UIAlertController with a preferredStyle of UIAlertControllerStyleAlert.

Midhun MP
  • 103,496
  • 31
  • 153
  • 200