简体   繁体   中英

Save image to device

Hi i cant seem to get his code to work

    //Save Image To Device
UIImage *image = [[UIImage alloc] initWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:[self.ImageURLS objectAtIndex:0]]]];
NSString *docDir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSLog(@"saving jpg");
NSString *jpgFilePath = [NSString stringWithFormat:@"%@/%@.jpg",docDir,[self.NameArray objectAtIndex:0]];
NSData *data2 = [NSData dataWithData:UIImageJPEGRepresentation(image, 1.0f)];//1.0f = 100% quality
[data2 writeToFile:jpgFilePath atomically:YES];

and i am getting the image via

    NSString *docDir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
    NSString *jpgFilePath = [NSString stringWithFormat:@"%@/%@.jpg",docDir,[self.NameArray objectAtIndex:[indexPath row]]];
    UIImage *image = [UIImage imageNamed:jpgFilePath];

Any help ?

Thanks

Change the last line of your code to

UIImage *image = [UIImage imageWithContentsOfFile:jpgFilePath];

UIImage's +imageNamed: searches the app bundle for a file with a given name, which is not what you need.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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