Home > Mobile >  How to convert an image taken with captureOutput didFinishProcessingPhoto into a string to be stored
How to convert an image taken with captureOutput didFinishProcessingPhoto into a string to be stored

Time:10-16

I am taking a photo and processing it using captureOutput:didFinishProcessingPhoto.

I need to convert the photo (AVCapturePhoto) into a string so I can send it to the server in a JSON. I need to store it in the userDefaults first, and on the last screen I need to retrieve it and add it to a JSON/Dictionary.

I'm working on a project I didn't start. I would normally have set this up completely different. Either way this is my problem now.

I'm trying this:

- (void)captureOutput:(AVCapturePhotoOutput *)output didFinishProcessingPhoto:(AVCapturePhoto *)photo error:(NSError *)error {
    if (error != nil){
        NSLog(@"%s: %@", "Error in capture process", error.debugDescription);
    }
    
    NSData *imageData = [photo fileDataRepresentation];
    
    if (imageData == nil){
        NSLog(@"%s", "unable to create image data");
    }
    
    NSData *photoData = UIImageJPEGRepresentation([[UIImage alloc] initWithData:imageData], 0.0);

    NSString *str = [[NSString alloc] initWithData:photoData encoding:NSUTF16StringEncoding];

//    UIImage *image = [[UIImage alloc] initWithData:imageData];
//    NSData *imageDataObject = UIImageJPEGRepresentation(image, 0.0);
    
//    NSString *photoString = [[NSString alloc] initWithData:imageDataObject encoding:NSUTF8StringEncoding];

//    UIImage *image = [[UIImage alloc] initWithData:imageData];
//    NSData *imageDataObject = UIImageJPEGRepresentation(image, 0.0);
//    NSString *photoString = [[NSString alloc] initWithData:imageDataObject encoding:NSUTF8StringEncoding];

    
    NSUserDefaults *myDefaults = [NSUserDefaults standardUserDefaults];
    [myDefaults setObject:str forKey:@"photo"];
}

commented out code is just failed attempts to do this.

In swift I implemented this by creating a codable object as a singleton (never stored it in defaults) and called this function on the data before sending it:

form.photo = UIImage(data: imageData)?.jpegData(compressionQuality: 0.0)

I'm having no such luck with objective c. I don't have a jpegData function....

Every time I try to convert the data the conversion fails and I get null back. Why?

CodePudding user response:

Thanks to @Larme for pointing me in the right direction. This is what I came up with:

- (void)captureOutput:(AVCapturePhotoOutput *)output didFinishProcessingPhoto:(AVCapturePhoto *)photo error:(NSError *)error {

    if (error != nil){
        NSLog(@"%s: %@", "Error in capture process", error.debugDescription);
    }
    
    NSData *imageData = [photo fileDataRepresentation];
    
    if (imageData == nil){
        NSLog(@"%s", "unable to create image data");
    }
    
    UIImage *image = [[UIImage alloc] initWithData:imageData];
    NSData *jpegImage = UIImageJPEGRepresentation(image, 0.0);
    NSString *base64Str = [jpegImage base64EncodedStringWithOptions:0];
    
    NSUserDefaults *myDefaults = [NSUserDefaults standardUserDefaults];
    [myDefaults setObject:base64Str forKey:@"photo"];
}
  • Related