2012年7月17日 星期二

Save URL jpg to file using Objective C in GNUStep

#import <Foundation/Foundation.h>
#import <Foundation/NSUrl.h>

int main (int argc, const char * argv[])
{
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
NSLog (@"hello world");
NSURL *url = [NSURL URLWithString:@"url contains myconetnt"];
NSError *theNetworkError;
    NSString *content = [NSString stringWithContentsOfURL:url encoding:NSASCIIStringEncoding error:&theNetworkError];
    NSInteger len= [content length];
    //NSLog(@"%@, contains %i characters",content, len);
    NSString *content2= [content substringToIndex:(len-3)];
    NSString *content3=[content2 stringByReplacingOccurrencesOfString:@" " withString:@""];
    NSLog(@"%@",content3);
    NSString *myImage = @"http://www.iphonedevsdk.com/forum/images/appads/blueprint.jpg";
NSString *tempImagemUrl = [myImage stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
NSURL *url1 = [NSURL URLWithString:tempImagemUrl];
NSData *mydata = [NSData dataWithContentsOfURL:url1];
NSString *fileName=@"test.jpg";
    BOOL b=[mydata writeToFile:fileName atomically:true];
[pool drain];
return 0;
}

沒有留言:

張貼留言