试图保存拍摄到appdocs的图像 - 没有发生

时间:2012-05-04 23:23:28

标签: objective-c nsstring uiimageview nsarray xcode4.3

我真的在努力解决这个问题,第三天无休止的尝试在我的应用中保存和加载图像。我正在从相机胶卷中挑选一张图片并尝试通过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 *appDocsDirectory& NSData *data所以我可能已经对此做了一个皇家哈希。

2 个答案:

答案 0 :(得分: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];
    }
}

希望你能从那里开始。 如果您需要任何解释或帮助,请随时提出

答案 1 :(得分:0)

我使用过NSuser默认值,所有已排序