2013-04-25 21 views
1

在我的xib我有一個UITextFieldUIImageView和一個按鈕,iPhone如何驗證字符串只是在Objective-C中的圖像名稱或圖像url?

enter image description here

究竟我想要的時候,用戶可以例如,輸入圖像的名字Default.png或完整的網絡URL的圖像,當按下該按鈕應檢查

如果文本很簡單,例如, Default.png然後使用本地圖像和 如果文本是帶有圖像的網址,則從圖像URL加載數據並創建一個圖像,然後放在UIImageView上。

我在這裏查了很多問題,但沒有得到我想要達到的確切解決方案。

我做了什麼:

- (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]; 
} 

即使在輸入正確的圖像URL它顯示我的警報「圖像不存在之後,從.m文件(這是不工作)

// imageViewViewController.h 

#import <UIKit/UIKit.h> 

@interface imageViewViewController : UIViewController<UITextFieldDelegate> 
{ 
    IBOutlet UITextField * urlInput; 
    IBOutlet UIImageView * myImgView; 
} 

- (IBAction)submit:(id)sender; 
@end 

代碼! !!」

我認爲我的URL驗證功能存在問題。

+0

只是因爲我輸入的網址(http://tempuri.org/swinsuit.png)它會通過您的驗證功能。但這並不意味着照片確實存在。 – 2013-04-25 11:31:33

+0

是的你是對的,我只想檢查用戶輸入的內容,URL或圖像名稱。 – 2013-04-25 11:54:05

回答

0

使用此代碼檢查其有效的URL或不...

NSURLRequest *req = [NSURLRequest requestWithURL:[NSURL URLWithString: url]]; 
bool valid = [NSURLConnection canHandleRequest:req]; 
+0

這會以不同的方式處理圖片名稱和圖片網址嗎? – 2013-04-25 11:32:35

+0

不,不是它的返回輸入的URL是有效的或由URLRequest類處理或不在..這裏如果它可以使用該有效的鈴聲變量而不是[self validateUrl:url]代碼.. :) – 2013-04-25 11:34:38

+0

完美!你能解釋我嗎? – 2013-04-25 11:41:35

1

代碼支持的字符串URL與否,

NSString *[email protected]"http://www.gmail.com"; 
NSString *[email protected]"http"; 
NSRange textRange = [incoming_string rangeOfString:substring]; 
    if(textRange.location != NSNotFound){ 
    NSLog(@"This is url image"); 
}else{ 
    NSLog(@"This is SYSTEM Image"); 
} 
+0

如果某些機構輸入以https://或www開頭的URL,該怎麼辦?或任何其他?你能爲此提出一些有效的regax嗎? – 2013-04-25 11:33:55

+0

在ios中。有效的URL僅以http或https開頭。https也可以找到這種方法。 – 2013-04-25 11:35:01

1
UIImage *img = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:txtField.text]]]; 
if(img) 
{ 
// Do something 
} 

嘗試這個。

1

使用此方法來檢查一個有效的URL

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

是的,它工作完美。你能解釋我實際上這個代碼在做什麼,因爲我是Objective-c和iPhone的新手。 – 2013-04-25 11:40:38

1

有兩種可選檢查有效的URL:

方法1:

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

方法2:

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

Hope這可能會解決問題