saving image to documents directory no longer works iOS8

寵の児 提交于 2019-12-22 11:22:54

问题


Saving images to the documents directory does not seem to working anymore in iOS8. I had hear that there was Changes To App Containers In iOS 8. But I still cant seem to get my code to work. I save an image to the documents directory, and retrieve it later to use. Here is my code for saving the image in the documents directory;

-(void) simpleCam:(SimpleCam *)simpleCam didFinishWithImage:(UIImage *)image {

if (![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
    //choose Photo

}else{

    if (image) {

        //save the image to photos library
        UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil);

        NSData *imageData = UIImageJPEGRepresentation(image,1);

        // Get image path in user's folder and store file with name image_CurrentTimestamp.jpg (see documentsPathForFileName below)
        NSString *imagePath = [self documentsPathForFileName:[NSString stringWithFormat:@"image_%f.jpg", [NSDate timeIntervalSinceReferenceDate]]];


        // Write image data to user's folder
        [imageData writeToFile:imagePath atomically:YES];

        // Store path in NSUserDefaults
        [[NSUserDefaults standardUserDefaults] setObject:imagePath forKey:[NSString stringWithFormat:@"%@%@",kImageDataKey,self.accessToken]];

        // Sync user defaults
        [[NSUserDefaults standardUserDefaults] synchronize];
    }
}

And my documentsPathForFileName method;

- (NSString *)documentsPathForFileName:(NSString *)name {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsPath = [paths objectAtIndex:0];

return [documentsPath stringByAppendingPathComponent:name];
}

The image path returns empty, but worked perfectly in iOS7.

来源:https://stackoverflow.com/questions/25978067/saving-image-to-documents-directory-no-longer-works-ios8

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!