Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/42.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone 正在尝试将jpeg保存到应用程序的文档目录_Iphone_Ios_Xcode_Uiimage - Fatal编程技术网

Iphone 正在尝试将jpeg保存到应用程序的文档目录

Iphone 正在尝试将jpeg保存到应用程序的文档目录,iphone,ios,xcode,uiimage,Iphone,Ios,Xcode,Uiimage,我有一个函数,它从一个URL获取一个图像,如下所示: - (UIImage *) getImageFromURL:(NSString *)fileURL { UIImage * result; NSData * img_data = [NSData dataWithContentsOfURL:[NSURL URLWithString:fileURL]]; result = [UIImage imageWithData:img_data]; return res

我有一个函数,它从一个URL获取一个图像,如下所示:

- (UIImage *) getImageFromURL:(NSString *)fileURL {
    UIImage * result;

    NSData * img_data = [NSData dataWithContentsOfURL:[NSURL URLWithString:fileURL]];
    result = [UIImage imageWithData:img_data];

    return result;
}
    -(void) saveImage:(UIImage *)image withFileName:(NSString *)imageName ofType:(NSString *)extension inDirectory:(NSString *)directoryPath {
    if ([[extension lowercaseString] isEqualToString:@"png"]) {
        [UIImagePNGRepresentation(image) writeToFile:[directoryPath stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.%@", imageName, @"png"]] options:NSAtomicWrite error:nil];
    } else if ([[extension lowercaseString] isEqualToString:@"jpg"] || [[extension lowercaseString] isEqualToString:@"jpeg"]) {
        [UIImageJPEGRepresentation(image, 1.0) writeToFile:[directoryPath stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.%@", imageName, @"jpg"]] options:NSAtomicWrite error:nil];
        NSLog(@"Image named %@ wrote to %@", imageName, directoryPath);
    } else {
        NSLog(@"Image Save Failed\nExtension: (%@) is not recognized, use (PNG/JPG)", extension);
    }
}
然后是一个从UIImage保存图像的函数,如下所示:

- (UIImage *) getImageFromURL:(NSString *)fileURL {
    UIImage * result;

    NSData * img_data = [NSData dataWithContentsOfURL:[NSURL URLWithString:fileURL]];
    result = [UIImage imageWithData:img_data];

    return result;
}
    -(void) saveImage:(UIImage *)image withFileName:(NSString *)imageName ofType:(NSString *)extension inDirectory:(NSString *)directoryPath {
    if ([[extension lowercaseString] isEqualToString:@"png"]) {
        [UIImagePNGRepresentation(image) writeToFile:[directoryPath stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.%@", imageName, @"png"]] options:NSAtomicWrite error:nil];
    } else if ([[extension lowercaseString] isEqualToString:@"jpg"] || [[extension lowercaseString] isEqualToString:@"jpeg"]) {
        [UIImageJPEGRepresentation(image, 1.0) writeToFile:[directoryPath stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.%@", imageName, @"jpg"]] options:NSAtomicWrite error:nil];
        NSLog(@"Image named %@ wrote to %@", imageName, directoryPath);
    } else {
        NSLog(@"Image Save Failed\nExtension: (%@) is not recognized, use (PNG/JPG)", extension);
    }
}
我面临的问题是,当我通过saveImage函数运行UIImage时,我认为它并没有保存图像。这是因为当我检查名为:@“Documents/filenamesavedas.jpeg”的UIImage图像时,它返回(null)。但是,图像下载不正确

以下是我的文档目录路径函数:

- (NSString *)getDocumentsDirectoryPath {
    NSString * documentsDirectoryPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
    NSLog(documentsDirectoryPath);
    return documentsDirectoryPath;
}
以下是我下载并尝试保存图像的位置:

   dispatch_async(jsonQueue, ^{
        // check to see if data authenticate runs successfully. 
    NSString *documentsDirectoryPath = [util getDocumentsDirectoryPath];
    UIImage *imageToSave = [util getImageFromURL:[properties objectForKey:@"current_group_logoURL"]];
    [util saveImage:imageToSave withFileName:[properties objectForKey:@"home_venue_logo"] ofType:@"jpeg" inDirectory:documentsDirectoryPath];
    NSLog(@"Group Logo URL %@", [properties objectForKey:@"current_group_logoURL"]);
    NSLog(@"Current Group Image %@", [properties objectForKey:@"home_venue_logo"]);        
        UIImage *testToSeeItHasDownloaded = imageToSave;
        NSString *imageLocation = [documentsDirectoryPath stringByAppendingPathComponent:[properties objectForKey:@"home_venue_logo"]];
        NSData *data = [NSData dataWithContentsOfFile:imageLocation];
        UIImage *testToSeeImageHasSaved = [UIImage imageWithData:data];
        NSLog(@"testToSeeImagehasDownloaded: %@", testToSeeItHasDownloaded);
        NSLog(@"image location to save to: %@", imageLocation);
        NSLog(@"testToSeeImagehasSaved: %@", testToSeeImageHasSaved );    

    });
控制台返回以下内容:

2012-10-22 16:27:45.642 asdaf[46819:1d807] Group Logo URL http://somesite.com/50a0e42619424ba551a956511c098656.jpeg
2012-10-22 16:27:45.642 asdaf[46819:1d807] Current Group Image 50a0e42619424ba551a956511c098656.jpeg
2012-10-22 16:27:45.649 asdaf[46819:1d807] testToSeeImagehasDownloaded: <UIImage: 0xb09ed50>
2012-10-22 16:27:45.649 asdaf[46819:1d807] testToSeeImagehasSaved: (null)
2012-10-22 16:27:45.642 asdaf[46819:1d807]集团徽标URLhttp://somesite.com/50a0e42619424ba551a956511c098656.jpeg
2012-10-22 16:27:45.642 asdaf[46819:1d807]当前组图像50a0e42619424ba551a956511c098656.jpeg
2012-10-22 16:27:45.649 asdaf[46819:1d807]TestToSeeImages已下载:
2012-10-22 16:27:45.649 asdaf[46819:1d807]testtoseeimagehasaved:(空)

TestToSeeMageHassaved应返回null。但我做错了什么?谢谢

我觉得这句话不对:

UIImage *testToSeeImageHasSaved = [UIImage imageNamed: [@"Documents/" stringByAppendingString: [properties objectForKey:@"home_venue_logo"]]];
我认为你必须做到:

UIImage* image = [UIImage imageWithContentsOfFile:"YOUR_FILE_PATH"];

[UIImage ImageName:“image_name”]
仅适用于附加到项目的图像

我觉得这行不合适:

UIImage *testToSeeImageHasSaved = [UIImage imageNamed: [@"Documents/" stringByAppendingString: [properties objectForKey:@"home_venue_logo"]]];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
//documentsDirectory is your documents directory path

//Now append your image name in this path
NSString *imagePath = [documentsDirectory stringByAppendingPathComponent:@"ImageName.jpeg"];

//convert your image in NSData
NSData *imageData = UIImagePNGRepresentation(image);
//Now write it at path
[imageData writeToFile:imagePath atomically:NO];
我认为你必须做到:

UIImage* image = [UIImage imageWithContentsOfFile:"YOUR_FILE_PATH"];
[UIImage ImageName:“image\u name”]
仅适用于附加到项目的图像

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
//documentsDirectory is your documents directory path

//Now append your image name in this path
NSString *imagePath = [documentsDirectory stringByAppendingPathComponent:@"ImageName.jpeg"];

//convert your image in NSData
NSData *imageData = UIImagePNGRepresentation(image);
//Now write it at path
[imageData writeToFile:imagePath atomically:NO];
您可以用同样的方法获取此图像

NSData *data = [NSData dataWithContentsOfFile:imagePath];
UIImage *image = [UIImage imageWithData:data];
编辑:

-(void)saveImage:(UIImage *)image withFileName:(NSString *)imageName ofType:(NSString *)extension 
{
   NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
   NSString *documentsDirectory = [paths objectAtIndex:0];
   NSString *imageName = [imageName stringByAppendingString:extension];
   //Note extension should be like .png, .jpg, .jpeg dont forget dot (.).

   NSString *imagePath = [documentsDirectory stringByAppendingPathComponent:imageName];

   NSData *imageData = UIImagePNGRepresentation(image);
   [imageData writeToFile:imagePath atomically:NO]; 
}
我现在不知道你的全部代码,但为什么你要传递一个
UIImage
对象,而你必须将你的图像保存在documents目录中。您也可以在方法中传递
NSData
。你做的更多

您的问题是每次保存.jpg图像时,以及在检索时,您试图在同一路径上查找.jpeg图像,这就是为什么会得到空值。在您的保存方法中查看此行-

[UIImageJPEGRepresentation(image, 1.0) writeToFile:[directoryPath stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.%@", imageName, @"jpg"]] options:NSAtomicWrite error:nil];
您可以用同样的方法获取此图像

NSData *data = [NSData dataWithContentsOfFile:imagePath];
UIImage *image = [UIImage imageWithData:data];
编辑:

-(void)saveImage:(UIImage *)image withFileName:(NSString *)imageName ofType:(NSString *)extension 
{
   NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
   NSString *documentsDirectory = [paths objectAtIndex:0];
   NSString *imageName = [imageName stringByAppendingString:extension];
   //Note extension should be like .png, .jpg, .jpeg dont forget dot (.).

   NSString *imagePath = [documentsDirectory stringByAppendingPathComponent:imageName];

   NSData *imageData = UIImagePNGRepresentation(image);
   [imageData writeToFile:imagePath atomically:NO]; 
}
我现在不知道你的全部代码,但为什么你要传递一个
UIImage
对象,而你必须将你的图像保存在documents目录中。您也可以在方法中传递
NSData
。你做的更多

您的问题是每次保存.jpg图像时,以及在检索时,您试图在同一路径上查找.jpeg图像,这就是为什么会得到空值。在您的保存方法中查看此行-

[UIImageJPEGRepresentation(image, 1.0) writeToFile:[directoryPath stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.%@", imageName, @"jpg"]] options:NSAtomicWrite error:nil];

当您将其保存在文档文件夹中时,它不会将其精确地保存在“document”目录中,而是类似于

path=/var/mobile/Applications/144B3628-5258-4939-8B80-006EC5BE45B2/Library/Caches/6_1.png
因此,要检索您的图像,您应该使用

NSString *uniquePath = [directory stringByAppendingPathComponent: filename];
image = [UIImage imageWithContentsOfFile: uniquePath];

其中directory是您用来保存它的路径,filename是您的jpeg的名称。

当您将它保存在文档文件夹中时,它不会确切地将它保存在“document”目录中,而是类似于

path=/var/mobile/Applications/144B3628-5258-4939-8B80-006EC5BE45B2/Library/Caches/6_1.png
[UIImage imageNamed:@"Documents/filenamesavedas.jpeg"]
因此,要检索您的图像,您应该使用

NSString *uniquePath = [directory stringByAppendingPathComponent: filename];
image = [UIImage imageWithContentsOfFile: uniquePath];
其中directory是用于保存它的路径,filename是jpeg的名称

[UIImage imageNamed:@"Documents/filenamesavedas.jpeg"]
不采用路径它会在包中搜索具有特定名称的文件

使用对NSFileManager的调用找到该文件,并使用预期的路径和文件名

不采用路径它会在包中搜索具有特定名称的文件


使用对NSFileManager的调用找到该文件,并使用预期的路径和文件名。

好的,我尝试了,但没有成功,我仍然得到了以下对象:UIImage*TestToSeeMageHassaved=[UIImage imageWithContentsOfFile:[@“Documents/”stringByAppendingString:[properties objectForKey:@“home\u Vincement\u logo]”;返回为null否,[@“Documents/”…]不是这样做的方法,您必须在保存图像的位置使用
file\u path
,例如
[directoryPath stringByAppendingPathComponent:[NSString stringWithFormat:@“%@.%@”,imageName,@“png”]
当然可以,您只需使用相同的
文件路径
即可从磁盘加载此图像OK我编辑了我的问题,以向您展示我现在正在做什么,但我仍然遇到相同的问题:(.好的,我试过了,但没用,我仍然得到了这个对象:UIImage*testToSeeImageHasSaved=[UIImage-imageWithContentsOfFile:[@“Documents/”string通过附加字符串:[properties-objectForKey:@“home\u-vention\u-logo”]];返回为空,[@“Documents/”…]不是这样做的,您必须在保存图像的位置使用
文件路径
,如下
[directoryPath stringByAppendingPathComponent:[NSString stringWithFormat:@“%@.%@”,imageName,@“png”]]
当然可以,当您想从磁盘加载此图像时,只需使用相同的
文件路径即可OK我编辑了我的问题以向您展示我现在正在做什么,但我仍然遇到相同的问题:(.我用修改过的代码编辑了我的问题,看它是否有效,我仍然有相同的问题。我用修改过的代码编辑了我的问题,看它是否有效,我仍然有相同的问题。我已将此答案合并到我的代码中,我仍然有相同的问题:(.正如您所说,您成功地获得了图像…在使用上述代码之后…删除用于保存图像的代码…当您理解它时,然后在代码中实现它。.首先尝试使用单个图像。:)我已将此答案纳入我的代码中,但我仍然遇到相同的问题:(.正如您所说,您成功地获得了图像…在使用上述代码后…删除用于保存图像的代码…当您理解它时,然后在代码中实现它。.首先尝试使用单个图像。:)现在检查我编辑的答案…您的问题已解决,我想:)现在检查我编辑的答案……我想你的问题已经解决了:)