将图像保存到设备

时间:2010-12-14 10:49:57

标签: iphone objective-c

嗨,我似乎无法让他的代码工作

    //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];

我正在通过

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

任何帮助?

由于

1 个答案:

答案 0 :(得分:1)

将代码的最后一行更改为

UIImage *image = [UIImage imageWithContentsOfFile:jpgFilePath];

UIImage的+imageNamed:在应用包中搜索具有给定名称的文件,这不是您需要的。