0

重複の可能性:
iOS のダウンロードとアプリ内の画像の保存

ファイルをダウンロードしたい。私はこれを見つけました

[NSData dataWithContentsOfURL:[NSURL URLWithString:@"http://lasp.colorado.edu/home/wp-content/uploads/2011/03/suncombo1.jpg"]]

私の質問は次のとおりです。

  1. ファイルはどこに保存されますか?

  2. スレッドを使用して、ダウンロード用のステータス バーを構築することは可能ですか?

  3. ファイルを保存するためにメモリ(内部/外部)を変更する方法はありますか?

今、私は使用しています

NSData *dataImage = [NSData dataWithContentsOfURL:[NSURL URLWithString:@"http://lasp.colorado.edu/home/wp-content/uploads/2011/03/suncombo1.jpg"]]; downloadStatus.text =@"size: %zd", malloc_size(dataImage);

結果は常に 32 です。これが実際の画像のサイズではないでしょうか。

4

5 に答える 5

1
  1. The file is not saved. It's loaded/retrieved and converted into an NSData object.
  2. Yes. However, if you're doing this you should look at NSURLConnection and particularly the NSURLConnectionDataDelegate protocol. You'll need to asynchronously download the file and get the callbacks into the delegate to be able to update your status bar. Or you could use a 3rd party networking library to simplify the whole thing, but it's good to understand what's going on behind the scenes.
  3. Yes. You can save the NSData object as a file when it's downloaded. If you were using Cocoa (not iOS) you could use NSURLDownload to download the file directly.
于 2012-12-13T06:03:01.680 に答える
0

Obj-C は初めてなので、まず NSURLConnection と NSURLConnectionDataDelegate (プロトコル) に慣れます。何が起こっているのかを理解できたら、AFNetworking などのネットワーク ライブラリに簡単に切り替えて、物事を簡素化できます。

于 2012-12-13T06:10:51.517 に答える
0
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
[request setDownloadProgressDelegate:myProgressIndicator];
[request startSynchronous];
NSLog(@"Max: %f, Value: %f", [myProgressIndicator maxValue],[myProgressIndicator doubleValue]);

また、このリンクをたどって理解を深めることもできます:) http://allseeing-i.com/ASIHTTPRequest/How-to-use

于 2012-12-13T06:02:42.760 に答える
0

You can assign the downloaded data to a variable and show it in an UIImageView as follows.

NSData *dataImage = [NSData dataWithContentsOfURL:[NSURL URLWithString:@"http://lasp.colorado.edu/home/wp-content/uploads/2011/03/suncombo1.jpg"]];
self.imageViewYours.image = [UIImage imageWithData:dataImage];
于 2012-12-13T06:03:50.333 に答える
0

このコードを試してみてください。画像が PhotoAlbum に保存されます。

   -(IBAction)save_Image:(id)sender
    {
        UIImageWriteToSavedPhotosAlbum([UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:[NSString stringWithFormat:@"Your URL"]]]], self, @selector(image:didFinishSavingWithError:contextInfo:), nil);

            [self performSelectorOnMainThread:@selector(imageDownloaded) withObject:nil waitUntilDone:NO ];

    }

- (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo
{
    if (error != NULL)
    {
        // handle error
    }
    else
    {
        // handle ok status
    }
}

- (void)imageDownloaded
{

      // network animation off
    [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;



    // do whatever you need to do after
}
于 2012-12-13T06:07:23.680 に答える