从URL Phonegap Iphone保存图像

时间:2010-12-24 11:49:47

标签: iphone cordova

我搜索了很多,但我找不到解决方案。

我需要一些方法来保存我的应用程序中的网络图片。就像一个函数,我可以把它放在网址上,它会下载它:

DownloadPhoto('http://www.myserver.com/myphoto.jpg');

有类似的东西吗?

2 个答案:

答案 0 :(得分:0)

这应该可以解决问题

- (void)downloadPhoto:(NSString*)urlToDownloadFrom
{
    // Download the image. now downloadeImg has the img you want.
    UIImage *downloadedImg = [UIImage imageWithData: [NSData dataWithContentsOfURL:
                               [NSURL URLWithString: urlToDownloadFrom]]];

    UIImageWriteToSavedPhotosAlbum(downloadedImg,self,
                         @selector(savedPhotoImage:didFinishSavingWithError:contextInfo:),NULL);
   // saves the image into your Photos.app/directory and calls the savedPhotoImage fucn. Am unclear if you actually needed this function. 

}

-(void)savedPhotoImage:(UIImage *)imagedidFinishSavingWithError:(NSError *)errorcontextInfo:(void *)contextInfo
{
}

答案 1 :(得分:-1)

pragma mark -

pragma mark用于加载图像的线程

-(void)loadImagesInBackground:(NSNumber *)index{

    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

    NSString *itemImagePath = [NSString stringWithFormat:@"http://www.myserver.com/myphoto.jpg"];

    if([itemImagePath length] == 0){
        NSString *imagePath = [[NSString alloc] initWithFormat:@"%@/%@",[[NSBundle mainBundle] resourcePath],@"Default.png"];
        UIImage *image = [[UIImage alloc] initWithContentsOfFile:imagePath];
        //[item setObject:image forKey:@"itemImage"];
        [imagePath release];
        [image release];
    }
    else {
        NSData *imageData = [NSData dataWithContentsOfURL:[NSURL URLWithString:itemImagePath]];
        if([imageData length] == 0){
            NSString *imagePath = [[NSString alloc] initWithFormat:@"%@/%@",[[NSBundle mainBundle] resourcePath],@"Default.png"];
            UIImage *image = [[UIImage alloc] initWithContentsOfFile:imagePath];
            //[item setObject:image forKey:@"itemImage"];
            [imagePath release];
            [image release];
        }
        //else if ([[item objectForKey:@"Icon"]isEqualToString:@""]){
//          NSString *imagePath = [[NSString alloc] initWithFormat:@"%@/%@",[[NSBundle mainBundle] resourcePath],@"Default.png"];
//          UIImage *image = [[UIImage alloc] initWithContentsOfFile:imagePath];
//          //[item setObject:image forKey:@"itemImage"];
//          [imagePath release];
//          [image release];
//          
//      }
//      
        else {
            UIImage *image = [[UIImage alloc] initWithData:imageData];
            providerImageView.image = image;
            //[item setObject:image forKey:@"itemImage"]; 
            [image release];
        }
    }

    [pool release];

}