-2

サーバーから 10 個の画像を含むフォルダーを取得し、そのフォルダーをドキュメント ディレクトリに保存したいと考えています。いくつかのコードを実行しましたが、実行すると、画像自体ではなく、画像の URL が取得されます。誰でも私を助けることができますか?

私のコード:

-(void)viewWillAppear:(BOOL)animated
{

NSMutableData *receivingData = [[NSMutableData alloc] init];
NSURL *url = [NSURL URLWithString:@"http://Someurl.filesCount.php"];
NSURLRequest *req = [NSURLRequest requestWithURL:url];


   NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:req delegate:self startImmediately:YES];

}

-(void) connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{

[receivingData appendData:data];
}
-(void) connectionDidFinishLoading:(NSURLConnection *)connection
{
NSError *error = nil;

{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *path = [paths objectAtIndex:0];
printf("\n the path is :%s",[path UTF8String]);


NSString *zipPath = [path stringByAppendingPathComponent:@"filesCount.php"];

[receivingData writeToFile:zipPath options:0 error:&error];



        NSString *documentsDirectoryPath = [[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"] stringByAppendingPathComponent:@"filesCount"];
        NSLog(@"the path %@",documentsDirectoryPath);



}
4

4 に答える 4

1

以前のプロジェクトでこの関数を作成しました。imageView と serverUrl を渡す必要があります。その後、自動的に imageView に画像を表示し、画像を一時ディレクトリに保存します。次に同じ画像を取得したい場合は、次にディスクから画像を取得します。

+(void)downloadingServerImageFromUrl:(UIImageView*)imgView AndUrl:(NSString*)strUrl{

NSFileManager *fileManager =[NSFileManager defaultManager];

NSString* theFileName = [NSString stringWithFormat:@"%@.png",[[strUrl lastPathComponent] stringByDeletingPathExtension]];
NSString *fileName = [NSHomeDirectory() stringByAppendingPathComponent:[NSString stringWithFormat:@"tmp/%@",theFileName]];


imgView.backgroundColor = [UIColor darkGrayColor];
UIActivityIndicatorView *actView = [[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite];
[imgView addSubview:actView];
[actView startAnimating];
CGSize boundsSize = imgView.bounds.size;
CGRect frameToCenter = actView.frame;
// center horizontally
if (frameToCenter.size.width < boundsSize.width)
    frameToCenter.origin.x = (boundsSize.width - frameToCenter.size.width) / 2;
else
    frameToCenter.origin.x = 0;

// center vertically
if (frameToCenter.size.height < boundsSize.height)
    frameToCenter.origin.y = (boundsSize.height - frameToCenter.size.height) / 2;
else
    frameToCenter.origin.y = 0;

actView.frame = frameToCenter;


dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
dispatch_async(queue, ^{

    NSData *dataFromFile = nil;
    NSData *dataFromUrl = nil;

    dataFromFile = [fileManager contentsAtPath:fileName];
    if(dataFromFile==nil){
        dataFromUrl=[[[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:strUrl]] autorelease];
    }

    dispatch_sync(dispatch_get_main_queue(), ^{

        if(dataFromFile!=nil){
            imgView.image = [UIImage imageWithData:dataFromFile];
        }else if(dataFromUrl!=nil){
            imgView.image = [UIImage imageWithData:dataFromUrl];
            // NSString *fileName = [NSHomeDirectory() stringByAppendingPathComponent:[NSString stringWithFormat:@"tmp/%@",theFileName]];

            BOOL filecreationSuccess = [fileManager createFileAtPath:fileName contents:dataFromUrl attributes:nil];
            if(filecreationSuccess == NO){
                NSLog(@"Failed to create the html file");
            }

        }else{
            imgView.image = [UIImage imageNamed:@"NO_Image.png"];
            imgView.tag = 105;
        }
        [actView removeFromSuperview];
        [actView release];
    });
});

}
于 2013-02-19T06:48:04.520 に答える
0

このコードを使用して、URLを使用して画像をダウンロードし、ドキュメントディレクトリに保存します。ダウンロードして保存する一連の画像のロジックを繰り返します。

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
        NSString *imageURL = @"http://sampleUrl";
        NSString *documentsDirectory = [paths objectAtIndex:0]; // Get documents folder
        UIImage *image = [[UIImage alloc] initWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:imageURL]]];

        NSString *imagePath = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"Image1.png"]];
        if(image != NULL)
        {
            //Store the image in Document
            NSData *imageData = UIImagePNGRepresentation(image);
            [imageData writeToFile: imagePath atomically:YES];
        }
于 2013-02-19T06:41:12.983 に答える
0

このコードを使用してみてください。

 NSURL* url = [NSURL URLWithString:@"http://imageAddress.com"];
 NSURLRequest* request = [NSURLRequest requestWithURL:url];


[NSURLConnection sendAsynchronousRequest:request
queue:[NSOperationQueue mainQueue]
completionHandler:^(NSURLResponse * response,
    NSData * data,
    NSError * error) {
if (!error){
        // do whatever you want with directory or store images.
    NSImage* image = [[NSImage alloc] initWithData:data];

}

}];
于 2013-02-19T06:28:27.960 に答える
0

サーバーから読み込まれた画像を UI に表示する場合は、

SDWebImageViewを試してみてください。UIButtonまたは UIImageView で使用でき、非常に簡単で効率的です。

例、

[yourImageView setImageWithURL:[NSURL URLWithString:@"http://www.domain.com/path/to/image.jpg"]
               placeholderImage:[UIImage imageNamed:@"placeholder.png"]];

使い方を読む?

さて、複数の画像の場合、(サーバー上の) すべての画像に共通のベース URL がある場合は、ループを実行する必要があるかもしれませhttp://yoururl/server/pictures/1.png2.png 3.png ... n.png。 、imageviewオブジェクトにロードして、後でドキュメントディレクトリに保存できます(SDWebImageViewデフォルトでは、この作業が自動的に行われることに注意してください)。これもオフにできます。

PS一度画像をロードし、ローカル(キャッシュ内)に保存します。次に同じ画像URLを渡すと、サーバーからロードされず、ローカルから画像が直接ロードされます。

于 2013-02-19T06:22:22.553 に答える