On my xib I have a UITextField
and UIImageView
and a button,
what actually I want is when user can enter image name e.g. Default.png
OR a full web URL
of image and when press that button should check
if text is simple e.g. Default.png then use the local image and if text is web URL with image then load data from image URL and create a image and then put on UIImageView.
I have checked many question here but did not get exact solution for what I want to achieve.
What I have done: (Which is not working)
// imageViewViewController.h
#import <UIKit/UIKit.h>
@interface imageViewViewController : UIViewController<UITextFieldDelegate>
{
IBOutlet UITextField * urlInput;
IBOutlet UIImageView * myImgView;
}
- (IBAction)submit:(id)sender;
@end
Code from .m file
- (IBAction)submit:(id)sender
{
NSString *url = urlInput.text;
UIImage *tempImg;
if([self validateUrl:url]) {
NSURL *imageurl = [NSURL URLWithString:url];
NSData *imagedata = [[NSData alloc]initWithContentsOfURL:imageurl];
tempImg = [UIImage imageWithData: imagedata];
} else {
tempImg = [UIImage imageNamed:url];
}
if(tempImg == nil){
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Image not present!!!" message:@"Image not present!!!" delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil];
[alert show];
} else {
myImgView.image = tempImg;
}
}
- (BOOL) validateUrl: (NSString *) candidate {
NSString *urlRegEx =
@"(http|https)://((\\w)*|([0-9]*)|([-|_])*)+([\\.|/]((\\w)*|([0-9]*)|([-|_])*))+";
NSPredicate *urlTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", urlRegEx];
return [urlTest evaluateWithObject:candidate];
}
Even after entering correct image URL it is showing me alert "Image not present!!!"
I think there is problem with my URL validation function.