开发者

Save image from URL Phonegap Iphone

I googled a lot, but I coul开发者_JAVA百科dnt find a solution.

I need some way to save pictures from the web on my application. Like a function, where I can just put the url and it will download it:

i.e

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

Is there something like this?


This should do the trick

- (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
{
}


pragma mark -

pragma mark Thread to load Images

-(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];
    
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜