0

我真的为此苦苦挣扎,在我的应用程序中保存和加载图像的无休止追求的第三天。我正在从相机胶卷中挑选一张图片,并尝试通过 appsdocs 目录将其保存到设备中。

在 Appdelegate.m 我有:

// Returns the URL to the application's Documents directory.
- (NSURL *)applicationDocumentsDirectory
{
    return [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject];

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
}

在我要保存和加载图像的类中(从相机胶卷中挑选到 UI imageView 中)

- (IBAction)save:(id)sender {
    UIImage *myImage = [imageView image];
    NSData *data = UIImagePNGRepresentation(myImage);
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *appDocsDirectory = [paths objectAtIndex:0];
}

- (IBAction)load:(id)sender {
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *appDocsDirectory = [paths objectAtIndex:0];
    UIImage* thumImage = [UIImage imageWithContentsOfFile: [NSString stringWithFormat:@"%@/%@.png", appDocsDirectory, @"myNewFile"]];
}
@end

我还在这个类中导入了 Appdelegate.h,不确定是否需要或正确?现在我终于摆脱了所有错误并且没有抛出异常但现在我的问题是当我尝试保存图像并加载时没有任何反应它。我也收到黄色三角形,告诉我我有未使用的负载 UIImage* thumImage NSString *appDocsDirectoryNSData *data所以我可能已经对此做了一个皇家哈希。

4

2 回答 2

1

这就是我在我的一个项目中的做法。

我使用 imagePickerController 的委托方法获取图片:

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
    pictureInfo = info;
}

然后我像这样保存它:

-(void)createPin
{
    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *pinFolderName = [NSString stringWithFormat:@"%f",[[NSDate date] timeIntervalSince1970]];
    NSString *pinFolderPath = [documentsDirectory stringByAppendingPathComponent:pinFolderName];
    [fileManager createDirectoryAtPath:pinFolderPath withIntermediateDirectories:YES attributes:nil error:NULL];
    self.fullsizePath = [pinFolderPath stringByAppendingPathComponent:@"fullsize.png"];
    NSString *mediaType = [pictureInfo objectForKey:UIImagePickerControllerMediaType];   
    if ([mediaType isEqualToString:@"public.image"]){
        UIImage *fullsizeImage = [pictureInfo objectForKey:UIImagePickerControllerEditedImage];
        NSData *fullsizeData = UIImagePNGRepresentation(fullsizeImage);
        [fullsizeData writeToFile:self.fullsizePath atomically:YES];
    }
}

希望你可以从那里开始。如果您需要任何解释或帮助,请随时询问

于 2012-05-05T00:34:31.143 回答
0

我使用了 NSuser 默认值,全部排序

于 2012-05-05T12:04:27.340 回答