Download data in String format and JPG images

Asked

Viewed 106 times

0

I am implementing a project, which has a Web Service. In this Web Service are saved product data, containing the fields: Name, Value, Description and Product Image.

I did some tests downloading only the data in String format, which due to the nature of the data, allows a quick download.

The idea of the project is that these downloaded data be saved in Core Data, where I am already successfully performing only with the data in String format.

However, the download of images only returns the URL from which the image is stored, and an independent request has to be made.

For the images, I will save them in the app directory, and save the address to disk (path file) in Core Data, not the URL and not the image file in Blob format.

However I tried to include the request of the image inside the looping that saves the files in Core Data, but due to different download speeds, I am having problems downloading the images.

To try to solve this problem I divided the request into two parts, a request for data in String format and another for images, I created two classes to handle it independently.

To integrate the classes, I inserted the image request inside the looping that requests the String data. However, due to the download time between the two, the looping that is a FOR triggers and terminates before downloading the first image, which generates a logic error by adding the disk addresses of the images in the wrong way.

Does anyone have any suggestions for this problem?

I would like to know how to share the class files, so that it can be evaluated. Because here would be very large.

Class: Gdrequesturl Utility: Download the data format String and return an Array containing the dictionary of the downloaded data.

Gdrequesturl. h

 #import <Foundation/Foundation.h>
    #import "URLGloblal.h"
    #import "GDResquestURLImage.h"
    @interface GDRequestURL : NSObject<NSURLConnectionDelegate>{
        NSMutableData *responseData;
        NSMutableDictionary *result;
        NSMutableArray*cleanArray;
        NSString* isPizza;
        NSString* isMassa;
        NSString* isRodizio;
        NSString* isBebidas;
        NSString* isEventos;
        // Download de imagem
        //GDResquestURLImage* imageRequest;
       }
    @property (nonatomic, strong) NSString* url;
    @property (nonatomic, strong) NSString* requestToken;
    @property (nonatomic, strong) NSString* httpHeader;
    -(void)getDataFromURL;
    -(NSMutableArray*)retornaResultado;
    @end

Gdrequesturl. m

#import "GDRequestURL.h"

@implementation GDRequestURL
- (id)init {

    if (self = [super init]) {

        isEventos = urlEventos;
        isPizza   = urlPizzas;
        isMassa   = urlMassas;
        isBebidas = urlBebidas;
        isRodizio = urlBebidas;

        _requestToken = urlGlobalToken;
        _httpHeader = urlGlobalHttpHeader;


        // Passo 1 Download image
        GDResquestURLImage*imageRequest = [[GDResquestURLImage alloc] init];
        [imageRequest getImageFromURL];

    }
    return self;
}


-(void)getDataFromURL{

    // Aqui é executado todo os métodos desta classe e retorna o resultado da consulta em forma de NSMutableDictionary.
    // Here runs all the methods of this class and returns the query result in the form of NSMutableDictionary.

    [self requestURLApi:_url requestValue:_requestToken httpHeaderField:_httpHeader];

    NSLog(@"getDataFromURL");

}



-(void)requestURLApi:(NSString *)requestURLString requestValue:(NSString*)value httpHeaderField:(NSString*)headFiedl{

     NSLog(@"requestURLApi");


    NSMutableURLRequest *req = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:requestURLString]];

    [req setValue:value forHTTPHeaderField:headFiedl];

    NSURLConnection *conn = [[NSURLConnection alloc] initWithRequest:req delegate:self];

    [conn start];

}


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

       NSLog(@"Connection pega data");

    responseData = [[NSMutableData alloc] init];
     NSError* error;

    [responseData appendData:data];

    result = [NSJSONSerialization JSONObjectWithData:responseData
                                                 options:NSJSONReadingMutableContainers error:&error];

}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection {

    NSLog(@"Connection did finish loading");
     // Aqui deve ser implementada a logica para descompactação dos dados de seu JSON.
     // Mantenha a lógica apenas acrecentando campos (chaves) conforme o seu JSON gerado na Goldark

     // Here the logic should be implemented for unpacking the data from your JSON.
     // Keep logic just prepending the name and fields (keys) as your JSON generated in Goldark

    // Este modelo é somente para exemplo.
    // This model is only for example.

#pragma mark - Se Pizzas
    if([isPizza isEqual:_url]){
    // Recebe o array Data
    NSMutableArray* arrayDataReceived =  [result objectForKey:@"data"];
    cleanArray = [[NSMutableArray alloc]init];

    for ( NSMutableDictionary* valuesDict in arrayDataReceived) {

        NSMutableDictionary* cleanDict = [[NSMutableDictionary alloc]init];

        [cleanDict setObject: [valuesDict objectForKey:@"nome"] forKey:@"nome"];
        [cleanDict setObject: [valuesDict objectForKey:@"valorquarto"]forKey:@"valorquarto"];
        [cleanDict setObject: [valuesDict objectForKey:@"valormedio"]forKey:@"valormedio"];
        [cleanDict setObject: [valuesDict objectForKey:@"valorgrande"]forKey:@"valorgrande"];
        [cleanDict setObject: [valuesDict objectForKey:@"descricao"]forKey:@"descricao"];

        // Pega URL da imagem baixa e salva em disco e retorna o path em disco para o Core Data.

       // GDResquestURLImage*imageRequest = [[GDResquestURLImage alloc] init];
       //  imageRequest.urlImage = [valuesDict objectForKey:@"imagem"];
       // [imageRequest getImageFromURL];

       // [cleanDict setObject:  [imageRequest  retornaImagem] forKey:@"imagem"];

            // Armazena os dicionarios em um Array
        [cleanArray addObject:cleanDict];

     }
       //  NSLog(@"%@",cleanArray);
   }
}

-(NSMutableArray*)retornaResultado{
    NSLog(@"retorna resultado");
    NSMutableArray* array =[ [NSMutableArray alloc] initWithArray:cleanArray];
    return array;
}
@end

Below is the class responsible for downloading the images, it is identical to String. It has the same functionality, download the images, save to disk and return the file path to the image file on disk.

Gdrequesturlimage. h

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

@interface GDResquestURLImage : NSObject<NSURLConnectionDelegate>{
NSMutableData *responseData;
NSMutableDictionary *result;
UIImage* imagemRecebida;
NSString *caminhoImagemEmDisco;
NSMutableDictionary* dicionarioCaminhoImagemEmDisco;
int numero;

}

@property (nonatomic, strong) NSString* urlImage;
@property (nonatomic, strong) NSString* requestToken;
@property (nonatomic, strong) NSString* httpHeader;

-(void)getImageFromURL;
-(NSString*)retornaImagem;

-(id)initWithURLToRequest:(NSString*)urlImage requestValue:(NSString*)requestToken httpHeaderField:(NSString*)httpHeader;

@end

Gdrequesturlimage. m

#import "GDResquestURLImage.h"


@interface GDResquestURLImage ()

@end

@implementation GDResquestURLImage
-(id)initWithURLToRequest:(NSString*)urlImage requestValue:(NSString*)requestToken httpHeaderField:(NSString*)httpHeader {

    if (self = [super init]) {

        numero = 0;

        NSMutableURLRequest *req = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:urlImage]];

        [req setValue:requestToken forHTTPHeaderField:httpHeader];

        NSURLConnection *conn = [[NSURLConnection alloc] initWithRequest:req delegate:self];

        [conn start];



    }

    return self;
}


-(void)getImageFromURL{

    // Aqui é executado todo os métodos desta classe e retorna o resultado da consulta em forma de NSMutableDictionary.
    // Here runs all the methods of this class and returns the query result in the form of NSMutableDictionary.

    [self requestURLApi:_urlImage requestValue:_requestToken httpHeaderField:_httpHeader];

    NSLog(@"1 -Inicio de download de imagem...\n");
}

-(void)requestURLApi:(NSString *)requestURLString requestValue:(NSString*)value httpHeaderField:(NSString*)headFiedl{

    NSMutableURLRequest *req = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:requestURLString]];

    [req setValue:value forHTTPHeaderField:headFiedl];

    NSURLConnection *conn = [[NSURLConnection alloc] initWithRequest:req delegate:self];

    [conn start];

}

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

    NSLog(@"2 - Connection DidReceiveData.\n");

    // As imagens ja vem em formato binario, portanto basta passar para responder (Tipo: NSMutableData).

    responseData = [[NSMutableData alloc] init];
    [responseData appendData:data];

}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection {

    NSLog(@"3 - ConnectionFinishLoading.\n");
    NSLog(@"Fim de conexao");

    for (UIImage* imagemTemp in responseData) {

    imagemRecebida = [[UIImage alloc]init];

    imagemRecebida = imagemTemp;

    // paga endereço da pasta
    NSString* enderecoPastaDisco = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];

    NSString* nomeImagem = [NSString stringWithFormat:@"%dTeste",numero];

     caminhoImagemEmDisco = [NSString stringWithFormat:@"%@/%@.jpg",enderecoPastaDisco,nomeImagem];
    NSData *data2 = [NSData dataWithData:UIImageJPEGRepresentation(imagemRecebida, 1.0f)];//1.0f = 100% quality
    [data2 writeToFile:caminhoImagemEmDisco atomically:YES];
    // retorna imagem

    NSLog(@"CaminhoImagem: %@",caminhoImagemEmDisco);


    numero ++;
    }
}

-(NSString*)retornaImagem{


    return caminhoImagemEmDisco;

}

-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error{

    NSLog(@"Erro de request: %@",error);
}
@end

Since I’m running constant tests to try to solve it, there are variables that won’t make sense apparently. But the code as a whole can be understood.

  • Tiago, if you put the code used to control downloads it is easier to help.

  • 1

    Just one question: what is WB?

  • @Tiagoamaral I think it is okay to paste here. The code field has scroll and everything is right.

  • Blz will be adding then... only 2 min

  • Added code, as I commented there are variables and comments in points of the code, which may not make sense because I am testing the code in several ways. I suggest creating a chat for this issue...

2 answers

0


I bypassed this impasse by downloading asynchronous images and independent of the Strings download. But consecutive to the end of the download of Strings. After downloading all the objects, a separation of the URL’s is made in an array that is passed to the second class that downloads and saves the images on disk, returned to the Core Data the path of the images of each object.

I used an additional field, ID to search the data, and a fetch to update the database, thus inserting the image path after downloading.

0

copy string.

NSData * dadosBrutos=[NSData dataWithContentsOfURL:[NSURL URLWithString: @"http://url.com.br/lista_noticias_db_json.php"]];
NSString* newStr = [[NSString alloc] initWithData:dadosBrutos encoding:NSUTF8StringEncoding];

copy image..

NSData * imageData=[NSData dataWithContentsOfURL:[NSURL URLWithString: @"http://url.com.br/imagem.png"]];
UIImage *img=[UIImage imageWithData:imageData];

Browser other questions tagged

You are not signed in. Login or sign up in order to post.