Bild im Format 800 x 600 speichern

Diese Seite verwendet Cookies. Durch die Nutzung unserer Seite erklären Sie sich damit einverstanden, dass wir Cookies setzen. Weitere Informationen

  • Bild im Format 800 x 600 speichern

    Hallo liebe Forummitglieder,

    bei meiner App mache ich ein Foto, das dann später per E-Mail versendet wird.
    Dies musste ich kompremieren, jedoch kommt jetzt jedes Bild im 640 x 480 Format an.
    Ich brauche aber 800 x 600

    Hier mein Code:

    Das Bild vom UIImagePickerController holen:


    Quellcode

    1. picker1.image = [info objectForKey:UIImagePickerControllerOriginalImage];
    2. [self saveImage:[self.picker1 image] withFileName:(@"") ofType:@"jpg"];


    Die Eigentliche konvertierung ins kleinere Format:

    Quellcode

    1. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    2. NSString *directoryPath = [paths objectAtIndex:0];
    3. NSString *imageFile = [[NSString alloc] init];
    4. imageFile = [directoryPath stringByAppendingPathComponent:[NSString stringWithFormat:@"%@_01.jpg", artikel.text]];
    5. UIGraphicsBeginImageContext(CGSizeMake(800, 600));
    6. [image drawInRect: CGRectMake(0, 0, 800, 600)];
    7. UIImage *smallImage = UIGraphicsGetImageFromCurrentImageContext();
    8. UIGraphicsEndImageContext();


    Zugriff auf das Bild um es per E-Mail zu verschicken:

    Quellcode

    1. UIImage *myImage1 = [UIImage imageWithContentsOfFile:[NSString stringWithFormat:@"%@/Documents/%@_01.jpg",NSHomeDirectory(), artikel.text]];
    2. NSLog(@"%@", [NSString stringWithFormat:@"%@/Documents/%@_01",NSHomeDirectory(), artikel.text]);
    3. NSData *imageData1 = UIImagePNGRepresentation(myImage1);
    4. [controller addAttachmentData:imageData1 mimeType:@"image/png" fileName:[NSString stringWithFormat:@"%@_01.jpg",artikel.text]];
    5. [UIImageJPEGRepresentation(smallImage, 1.0) writeToFile:imageFile options:NSAtomicWrite error:nil];


    Danke im Voraus für eure Antworten.

    Grüße Mario
  • Marioheld schrieb:

    [self saveImage:[self.picker1 image] withFileName:(@"") ofType:@"jpg"];

    Wieso mit leerem Filenamen speichern ?

    Marioheld schrieb:

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *directoryPath = [paths objectAtIndex:0];
    NSString *imageFile = [[NSString alloc] init];

    imageFile = [directoryPath stringByAppendingPathComponent:[NSString stringWithFormat:@"%@_01.jpg", artikel.text]];

    UIGraphicsBeginImageContext(CGSizeMake(800, 600));
    [image drawInRect: CGRectMake(0, 0, 800, 600)];


    Hm Du erstellst dir deinen Pfad zum Image (imageFile) und benutzt dann image. Woher kommt image ? ich schätze es ist sowas wie

    Quellcode

    1. UIImage *image = [UIImage imageWithContentsOfFile....
    ?
  • abitcocoa schrieb:

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *directoryPath = [paths objectAtIndex:0];
    NSString *imageFile = [[NSString alloc] init];

    imageFile = [directoryPath stringByAppendingPathComponent:[NSString stringWithFormat:@"%@_01.jpg", artikel.text]];

    UIGraphicsBeginImageContext(CGSizeMake(800, 600));
    [image drawInRect: CGRectMake(0, 0, 800, 600)];
    Das gehört zu der Methode: saveImage:(UIImage *)image withFileName:(NSString *)imageName ofType:(NSString *)extension
    Dort wird auch UIImage image festgelegt.
    Der Methode kann man einem FileName geben, aber ich setzte den Namen schon in der Methode, daher muss ich ihn nicht übergeben, obwohls schon besser wäre wenn ich den Filename mitgebe. (Des ändere ich gleich)
  • Ok ich habe mir für sowas diese Methode geschrieben, die eigentlich immer gut funktioniert hat bis jetzt:

    Quellcode

    1. + (UIImage *)creatImage:(UIImage *)img scaledSize:(CGSize)size {
    2. UIGraphicsBeginImageContextWithOptions(size, NO, 0.0);
    3. [img drawInRect:CGRectMake(0, 0, size, size.height)];
    4. UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    5. UIGraphicsEndImageContext();
    6. return image;
    7. }