1

这是一个简单的照片杂志。选择一张照片,添加标题,将它们存储在一起,然后让用户打印。也许这甚至不是正确的方法,但有一些事情我可以使用一些指导:(1)如何将图像和文本数据存储在一起以便可打印,(2)如果您放置多张照片/我如何在条目之间输入返回以分隔它们,以及(3)有没有办法将图像和标题捕获一起以避免重复所有文件标识等。现在我保存了图像,但没有标题。我知道这些是混合类型,但不知道解决方案。编码新手。

- (void)viewDidLoad
{
    [super viewDidLoad];

    // User picks a photo from the their phone camera roll
    // Since iPhone simulator doesn't have photos, load and display a placeholder image
    NSString *fPath = [[NSBundle mainBundle] pathForResource:@"IMG_1588" ofType:@"jpg"];
    url = [NSURL fileURLWithPath:fPath];
    [webView loadRequest:[NSURLRequest requestWithURL:url]];

    //Save image to file
    UIImage *image = [UIImage imageWithContentsOfFile:fPath];
    NSArray *DocumentsDirectoryPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *path = [DocumentsDirectoryPath objectAtIndex:0];
    NSString *filePath = [path stringByAppendingPathComponent:@"image.jpeg"];
    NSData *data2 = [NSData dataWithData:UIImageJPEGRepresentation(image, 1.0f)];//1.0f = 100% quality
    [data2 writeToFile:filePath atomically:YES];
}

- (IBAction)Button:(id)sender {

    //Display keyboard and image caption from user
    NSString *caption = enterCaption.text;

    // Write the caption at the end of the image file
    NSArray *DocumentsDirectoryPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *path = [DocumentsDirectoryPath objectAtIndex:0];
    NSString *filePath = [path stringByAppendingPathComponent:@"image.jpeg"];
    NSFileHandle *myHandle = [NSFileHandle fileHandleForWritingAtPath:filePath];
    [myHandle seekToEndOfFile];
    [myHandle writeData:  [caption dataUsingEncoding:NSUTF8StringEncoding]];
    [myHandle closeFile];
}

    //Dismiss Keyboard
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [textField resignFirstResponder];
    return YES;
}

我也无法让if ([[NSFileManager defaultManager] fileExistsAtPath:filePath], NO)下面的这个版本的代码工作。对此非常不解。有什么线索吗?

if ([[NSFileManager defaultManager] fileExistsAtPath:filePath], YES) {
        //Writing image to the created file
        NSFileHandle *myHandle = [NSFileHandle fileHandleForWritingAtPath:filePath];
        // move to the end of the file to add data
        [myHandle seekToEndOfFile];
        NSLog(@"caption written on existing file =%@", caption);
        [myHandle writeData:  [caption dataUsingEncoding:NSUTF8StringEncoding]];
        [myHandle closeFile];}
     else {
4

0 回答 0