2011-03-03 124 views
3

你好朋友,我想比較兩個網址在xcode(目標c) 怎麼可能?比較兩個網址(iPhone)

-(IBAction) download{ 
NSURL *url1 = [NSURL URLWithString:@"http://www.google.com/images?hl=en&q=flowers&bav=on.1,or.&um=1&ie=UTF-8&source=og&sa=N&tab=wi&biw=1512&bih=677"]; 
NSURL *url2 = [NSURL URLWithString:@"http://www.google.com/imgres?imgurl=http://www.missouriplants.com/Yellowopp/Helianthus_divaricatus_flowers.jpg&imgrefurl=http://www.missouriplants.com/Yellowopp/Helianthus_divaricatus_page.html&usg=__W-J2oEuDpbxhlU4plpC3JHP3wU0=&h=449&w=450&sz=41&hl=en&start=0&zoom=1&tbnid=axuO9VsiGsWlCM:&tbnh=131&tbnw=134&ei=h9tsTebwLc3irAeW1Kz7Bg&prev=/images%3Fq%3Dflowers%26um%3D1%26hl%3Den%26sa%3DN%26biw%3D1512%26bih%3D677%26tbs%3Disch:1&um=1&itbs=1&iact=rc&dur=426&oei=h9tsTebwLc3irAeW1Kz7Bg&page=1&ndsp=33&ved=1t:429,r:0,s:0&tx=57&ty=98"]; 
url = [webView.request URL];  
NSLog(@"urlrecieved .............................. %@", url); 
NSLog(@"urlrecieved 1 ............................. %@", url1); 
NSLog(@"urlrecieved 2.............................. %@", url2); 

if([url isEqualToString:url1]) 
{ 
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"warning" message:@"select an image" delegate:nil cancelButtonTitle:@"ok" otherButtonTitles:nil]; 
    [alert show]; 
    [alert release]; 
} 
else if([url isEqualToString:url2]) 
{ 
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"warning" message:@"select an image" delegate:nil cancelButtonTitle:@"ok" otherButtonTitles:nil]; 
    [alert show]; 
    [alert release]; 
} 
else 
{  
NSLog(@"Downloading..."); 
NSLog(@"url recieved: %@", url); 
UIImage *image = [[UIImage alloc] initWithData:[NSData dataWithContentsOfURL:url]]; 
NSLog(@"%f,%f",image.size.width,image.size.height); 
NSString *docDir = @"/Users/gauravmurghai/Desktop"; 
// If you go to the folder below, you will find those pictures 
    NSLog(@"%@",docDir); 
    NSLog(@"saving png"); 
// NSString *pngFilePath = [NSString stringWithFormat:@"%@/neetu%d.png",docDir , r]; 
//  r++; 

    NSString *pngFilePath = [NSString stringWithFormat:@"%@/flower.png",docDir]; 
    NSData *data1 = [NSData dataWithData:UIImagePNGRepresentation(image)]; 
    [data1 writeToFile:pngFilePath atomically:YES]; 
    [image release]; 
} 

}

回答

3

它實際上需要一點點工作,以絕對確保兩個URL是相同的。看看這篇博客文章,解釋更多。

NSURL Equality

12

基類iOS的對象分層結構,NSObject的的,具有由子類ovverrided提供平等檢查方法的isEqual。 NSUrl將提供它自己的實現,所以你不必擔心轉換爲NSString來執行比較。

NSURL *url1 = [NSURL URLWithString:@"http://www.google.com"]; 
NSURL *url2 = [NSURL URLWithString:@"http://www.google.com"]; 
if ([url1 isEqual:url2]) { 
...... 
+0

感謝,這對我幫助很大 – user642472 2011-03-03 08:00:48

+2

擡起頭 - 這可能是兩個NSURLs指向相同的資源,而不是平等使用的前綴,因爲字符的字符(文件:///,文件: // localhost /)中這讓我絆倒了,並造成了一個錯誤。對我來說,比較'''''lastPathComponent'''''''''''''''''''''''絕對路徑''' – 2014-04-30 20:09:11