1

On my xib I have a UITextField and UIImageView and a button,

enter image description here

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.

4

6 回答 6

2

You could use the

+(id)URLWithString:(NSString *)URLString 

method of NSURL, which returns nil if the string is not URL

"The string with which to initialize the NSURL object. Must be a URL that conforms to RFC 2396. This method parses URLString according to RFCs 1738 and 1808." (c) https://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSURL_Class/Reference/Reference.html

于 2013-04-25T11:26:21.507 回答
1

The code support the string is URL or Not,

NSString *incoming_string=@"http://www.gmail.com";
NSString *substring=@"http";
NSRange textRange = [incoming_string rangeOfString:substring];
  if(textRange.location != NSNotFound){
    NSLog(@"This is url image");
}else{
    NSLog(@"This is SYSTEM Image");
}
于 2013-04-25T11:29:51.273 回答
1
UIImage *img = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:txtField.text]]];
if(img)
{
// Do something
}

Try this.

于 2013-04-25T11:33:51.767 回答
1

Use this method to check for a valid url

- (BOOL)isValidUrl:(NSString *)urlString{
    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:urlString]];
    return [NSURLConnection canHandleRequest:request];
}
于 2013-04-25T11:36:09.733 回答
1

There are two alternative to check valid URL :

Method 1:

- (BOOL)CheckUrl:(NSString *)urlString {
    NSURLRequest *req = [NSURLRequest requestWithURL:[NSURL URLWithString:urlString]];
    return [NSURLConnection canHandleRequest:request];
} 

Method 2 :

NSString *urlRegEx = @"http(s)?://([\\w-]+\\.)+[\\w-]+(/[\\w- ./?%&amp;=]*)?";

Hope this might solve the problem

于 2013-04-25T12:20:13.167 回答
0

Use this code for check that its valid URL or not...

NSURLRequest *req = [NSURLRequest requestWithURL:[NSURL URLWithString: url]];
bool valid = [NSURLConnection canHandleRequest:req];
于 2013-04-25T11:27:03.103 回答