Iphone 将图像保存到设备

Iphone 将图像保存到设备,iphone,objective-c,Iphone,Objective C,嗨,我好像不能让他的代码正常工作 //Save Image To Device UIImage *image = [[UIImage alloc] initWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:[self.ImageURLS objectAtIndex:0]]]]; NSString *docDir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirec

嗨,我好像不能让他的代码正常工作

    //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];
有什么帮助吗


谢谢

将代码的最后一行更改为

UIImage *image = [UIImage imageWithContentsOfFile:jpgFilePath];

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

我认为这与保存TBH有关。您的代码会给出错误/意外的结果吗?你能用调试器一步一步地检查它,看看每一步返回的值吗?不,它都是0警告等等,我只是让enver在之前保存了一个图像,所以我对itOK没有任何线索,检查
[NSURL URLWithString:[self.ImageURLS objectAtIndex:0]
返回的值,它看起来最可疑。同样奇怪的是,您将名称保存在索引0:[self.NameArray objectAtIndex:0]处,然后读取以下内容:[self.NameArray objectAtIndex:[indexPath row]]。[indexPath行]为零吗?图像文件名如何(请参阅前面的注释)?