我正在拍照并使用captureOutput:didFinishProcessingPhoto.
我需要將照片 (AVCapturePhoto) 轉換為字串,以便我可以將它以 JSON 格式發送到服務器。我需要先將它存盤在 userDefaults 中,在最后一個螢屏上我需要檢索它并將其添加到 JSON/Dictionary。
我正在做一個我沒有開始的專案。我通常會設定完全不同的。無論哪種方式,這都是我現在的問題。
我正在嘗試這個:
- (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 中,我通過創建一個可編碼物件作為單例(從未將其存盤在默認值中)并在發送資料之前在資料上呼叫此函式來實作這一點:
form.photo = UIImage(data: imageData)?.jpegData(compressionQuality: 0.0)
我對目標c沒有這樣的運氣。我沒有 jpegData 函式....
每次我嘗試轉換資料時,轉換都會失敗,并且回傳空值。為什么?
uj5u.com熱心網友回復:
感謝@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"];
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/329174.html
標籤:ios 目标-c avcapturephotooutput avcapturephotosettings
