简体   繁体   English

在iOS中下载和保存图像

[英]Downloading and saving images in iOS

I am creating an iPhone app and in that I want to download multiple image files from web service. 我正在创建一个iPhone应用程序,因此我想从Web服务下载多个图像文件。 And I want to save the images in the local folder in the app itself and save the path to each files in SQLite database. 我想将图像保存在应用程序本身的本地文件夹中,并将路径保存到SQLite数据库中的每个文件。 How can I implement this? 我该如何实施? Please help. 请帮忙。

**// Get an image from the URL below**
    UIImage *image = [[UIImage alloc] initWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:@"yorimageurl"]]];

    NSLog(@"%f,%f",image.size.width,image.size.height);

    **// Let's save the file into Document folder.**

    NSString *Dir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];

    NSString *pngPath = [NSString stringWithFormat:@"%@/test.png",Dir];// this path if you want save reference path in sqlite 
    NSData *data1 = [NSData dataWithData:UIImagePNGRepresentation(image)];
    [data1 writeToFile:pngFilePath atomically:YES];

    NSLog(@"saving jpeg");
    NSString *jpegPath = [NSString stringWithFormat:@"%@/test.jpeg",Dir];// this path if you want save reference path in sqlite 
    NSData *data2 = [NSData dataWithData:UIImageJPEGRepresentation(image, 1.0f)];//1.0f = 100% quality
    [data2 writeToFile:jpegFilePath atomically:YES];

    NSLog(@"saving image done");

    [image release];

>> Update Additional: >>更新附加:

You need to save image name(unique name that you can do early..right..!!) in sqlite database and you can make path or retrieve image using that name as i shown below. 您需要将图像名称(可以尽早使用的唯一名称..right .. !!)保存在sqlite数据库中,然后可以使用该名称创建路径或检索图像,如下所示。

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,     NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *getImagePath = [documentsDirectory stringByAppendingPathComponent:@"test.png"];// here you jus need to pass image name that you entered when you stored it.

Hope, this will help you... 希望这个能对您有所帮助...

After implementing @Nit's code.. you can refer This for insert record in database. 在实现@Nit的代码后,您可以参考以获取数据库中的插入记录。

This is showing how to insert record in DB. 这显示了如何在数据库中插入记录。 and for getting path read my comments in @Nit's answer. 为了获取路径,请阅读@Nit答案中的评论。

UPDATE: 更新:

For getting path of each downloaded file you have to keep unique name for each file. 为了获得每个下载文件的路径,您必须为每个文件保留唯一的名称。 Now you will have the path where you are writing file that is unique path for every file. 现在,您将拥有写入文件的路径,该路径是每个文件的唯一路径。 Now you simply need to execute insert query every time you download a file or need to save filepath in array and execute query later according to your requirement. 现在,您只需在每次下载文件时执行插入查询,或者需要将文件路径保存在数组中,然后根据需要稍后执行查询。 Don't forget to give different file name to every file otherwise only single file will be there and your path will be same. 不要忘记给每个文件都使用不同的文件名,否则只有一个文件,并且路径也相同。

For getting unique filename you can use timestamp: 为了获得唯一的文件名,您可以使用时间戳:

NSString *fileName = [NSString stringWithFormat:@"%lf.png", [[NSDate date] timeIntervalSince1970];

UPDATE2: UPDATE2:

You have path now like: /Users/John/Library/Application Support/iPhone Simulator/5.1/Applications/84A837AA-7881-4D99-B6E2-623DC9A2E5D3/Documents/test.png 您现在的路径类似于: /Users/John/Library/Application Support/iPhone Simulator/5.1/Applications/84A837AA-7881-4D99-B6E2-623DC9A2E5D3/Documents/test.png

For getting Image in Image view: 要在图像视图中获取图像:

UIImage *img = [UIImage imageWithContentsOfFile:yourPath];
//[UIImage imageWithData:[NSData dataWithContentsOfFile:yourPath]];
UIImageView *imgView = // Alloc + Init + Setting frame etc
imgView.image = img;

This will show image in your imageview 这将在您的imageview中显示图像

Hope this helps 希望这可以帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM