Translate

Tuesday, January 29, 2013

Download on image from another thread(GCD)

dispatch_queue_t downloadQueue = dispatch_queue_create("image", NULL);
    dispatch_async(downloadQueue, ^{
        NSData *imageData = [NSData dataWithContentsOfURL:[NSURL URLWithString:@"http://blogs-images.forbes.com/jasonevangelho/files/2013/01/iphone5.jpeg"]];
        dispatch_async(dispatch_get_main_queue(), ^{
            UIImage *image = [UIImage imageWithData:imageData];
            imageView.image = image;
            imageView.frame = CGRectMake(0, 0, image.size.width, image.size.height);
            scrollView.contentSize = image.size;
        });
    });
    dispatch_release(downloadQueue);

Monday, January 28, 2013

Downloading a file from url and saving to resources on iPhone


- (void)viewDidLoad
{
    [super viewDidLoad];
   self.fileData = [NSMutableData data];
NSString *file = [NSString stringWithFormat:@"http://en.wikipedia.org/wiki/Text_file"]; NSURL *fileURL = [NSURL URLWithString:file]; NSURLRequest *req = [NSURLRequest requestWithURL:fileURL]; NSURLConnection *conn = [NSURLConnection connectionWithRequest:req delegate:self]; } - (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response { [self.fileData setLength:0]; self.totalFileSize = [NSNumber numberWithLongLong:[response expectedContentLength]]; } - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data { [self.fileData appendData:data]; } - (void)connectionDidFinishLoading:(NSURLConnection *)connection { NSArray *dirArray = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSLog(@"%@", [dirArray objectAtIndex:0]); NSString *path = [NSString stringWithFormat:@"%@/blah.text", [dirArray objectAtIndex:0]]; if ([self.fileData writeToFile:path options:NSAtomicWrite error:nil] == NO) { NSLog(@"writeToFile error"); } else { NSLog(@"Written!"); } }