0

単純なフォト ジャーナルを作成するコードを作成しようとしています。画像を選択し、それをファイルに書き込み、テキストの説明を追加します。以下は私のコードです。画像またはテキストを書き込むことはできますが、両方を連続して書き込むことはできません。どちらか一方が他方を上書きします。

- (void)viewDidLoad
{
    [super viewDidLoad];

    // 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]];

//    UIImage *image = [UIImage imageNamed:@"IMG_1588.jpg"];

    // Create the file to write the image
    NSArray *DocumentsDirectoryPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *path = [DocumentsDirectoryPath objectAtIndex:0];
    NSString *filePath = [path stringByAppendingPathComponent:@"test.doc"];

    //Creating a file at this path
    NSFileManager *fileManager = [NSFileManager defaultManager];
    BOOL ok = [fileManager createFileAtPath:filePath contents:nil attributes:nil];
    if (!ok) {NSLog(@"Error creating file %@", filePath);}
    else {

    //Writing image to the created file
    NSFileHandle *myHandle = [NSFileHandle fileHandleForWritingAtPath:filePath];

    // move to the end of the file to add data
    [myHandle seekToEndOfFile];
//    [myHandle writeData:UIImageJPEGRepresentation(image, 1.0)];
    [myHandle closeFile];
    }
}
    // User provides a caption for the image
- (IBAction)Button:(id)sender {

    NSString *caption = enterCaption.text;

    NSArray *DocumentsDirectoryPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *path = [DocumentsDirectoryPath objectAtIndex:0];
    NSString *filePath = [path stringByAppendingPathComponent:@"test.doc"];

    ///Creating a file at this path
    NSFileManager *fileManager = [NSFileManager defaultManager];
    BOOL ok = [fileManager createFileAtPath:filePath contents:nil attributes:nil];
    if (!ok) {NSLog(@"Error creating file %@", filePath);}
    else {

        //Writing image to the created file
        NSFileHandle *myHandle = [NSFileHandle fileHandleForWritingAtPath:filePath];

        // move to the end of the file to add data
        [myHandle seekToEndOfFile];
        [myHandle writeData:  [caption dataUsingEncoding:NSUTF8StringEncoding]];
        [myHandle closeFile];
    }
}

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

@end
4

1 に答える 1

0

createFileAtPath を使用するためです。ファイルが存在しない場合にのみそれを使用し、それ以外の場合はファイルを開くだけです。

編集: no の場合はすべてのサイズをログに記録し、正常に表示されるかどうかを確認します。

于 2013-06-02T13:19:24.000 に答える