如何将使用captureOutput didFinishProcessingPhoto拍摄的图像转换为使用objecti



我正在拍摄一张照片,并使用captureOutput:didFinishProcessingPhoto进行处理。

我需要将照片(AVCapturePhoto)转换为字符串,以便我可以将其发送到JSON中的服务器。首先我需要存储userDefaults,最后屏幕上和我需要检索它并将它添加到一个JSON/字典。

我在做一个不是我开始的项目。我通常会做完全不同的设置。不管怎样,这都是我现在的问题。

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"];
}

注释掉的代码只是失败的尝试。

在swift中,我通过创建一个可编码的对象作为singleton(从未将其存储在默认值中)来实现这一点,并在发送数据之前调用该函数:

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

我没有这样的运气与objective c。我没有一个jpegData函数....

每次我尝试转换数据转换失败,我得到null返回。为什么?

感谢@Larme为我指明了正确的方向。这是我想出来的:

- (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"];
}

最新更新