Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UIImage encoded to NSData in ObjectiveC and then decoded in Swift

I have an app that was originally created in objective C (lets call this version 1) and I have now converted my app to Swift 2.0 (version 2). One of the main functions of the app is the ability to send images and text from one device to another. Images and text are stored in a NSMutableDictionary and then encoded to NSData and the sent / stored on the Parse backend server. The design of my app also has the ability to email an image from one device to another.

This is working well for both versions of my app – Objective C and Swift. Great !

My problem is when a user sends NSData from version 1 of my app to a device with version 2 (basically an image encoded in objective C and then decoded in Swift) !! Encoded text decodes fine but not the image (saved as objectForKey("data")). See below example. quizData is an array the holds dictionary (keyValue items) that have been sent from another device. This array works with all items except for objectForKey("data"). This object is the encoded image.

 var imageData = NSData()

imageData = quizData.objectAtIndex(currentQuestionNumber).objectForKey("data") as! NSData

  // the following always prints out lots of info to confirm the imageData has the encoded image
  print("imageData.length = \(imageData.length)")
  print("imageData.description = \(imageData.description)")

   // decoding
  photoImageView.image = UIImage(data:imageData)

ok, so the above works when the image was created on another device using Swift. But if the image created and sent from version 1 (objective c) the photoImageView is blank (no errors) yet the imageData is huge (the printout shows that imageDate does hold the users image).

Surley if an NSdata object has the data for a UIImage it should be able to be decoded in ObjC or Swift ?? No problem sending more code if required

Question amended as follows : Not sure if this really helps but heres objC code for sending a NSData via email (all app data is saved a pList)

// emailArray to be populated with selected data from plist
NSMutableArray *emailArray = [NSMutableArray arrayWithContentsOfFile:path];

 MFMailComposeViewController *emailPicker = [[MFMailComposeViewController alloc]init];
 emailPicker.mailComposeDelegate =self;


/// NSdata from emailArray
NSData *emailQuizData = [NSKeyedArchiver  archivedDataWithRootObject:emailArray];
[emailPicker addAttachmentData:emailQuizData mimeType:@"application/quizApp" fileName:_quizNameLabel.text];
like image 722
pete Avatar asked Apr 18 '16 11:04

pete


1 Answers

if you use base64 encoding this issue shouldn't arise.

here is the implementation in swift:

import UIKit

func base64StringForImage(image: UIImage) -> String? {
    guard let data = UIImagePNGRepresentation(image) else { return nil }
    return data.base64EncodedStringWithOptions([])
}

func imageFromBase64String(string: String) -> UIImage? {
    guard let data = NSData(base64EncodedString: string, options: []) else { return nil }
    return UIImage(data: data)
}

here is the implementation in objc:

#import <UIKit/UIKit.h>

NS_INLINE NSString * base64StringForImage_objc(UIImage *image) {
    NSData *imageData = UIImagePNGRepresentation(image);
    return [imageData base64EncodedStringWithOptions:0];
}

NS_INLINE UIImage * imageFromBase64String_objc(NSString *string) {
    NSData *imageData = [[NSData alloc] initWithBase64EncodedString: string options: 0];
    return [[UIImage alloc] initWithData:imageData];
}
like image 140
Casey Avatar answered Oct 13 '22 03:10

Casey