How to get UIImage from NSDictionary in objective c - ios

I have searched more and more but didn't get the perfect solution.
I have saved an image in a dictionary.
#{
......
#"PrdImg":[UIImage imageNamed:[image objectAtIndex:indexPath.row]
......
};
The image data saved perfectly. I checked it.
But when I want to get this data from the dictionary then an exception encountered. The retrieving code is giving below.
PoctImage.image = [UIImage imageNamed:PrdDetailsDic[#"PrdImg"]];
The encountered exception is -
'NSInvalidArgumentException', reason: '-[UIImage length]: unrecognized selector sent to instance 0x7f9428e4e930'
I have also read more document about the exception. But didn't get the specific solution.
Now I want to know how can I get the image data from the dictionary. What is the specific syntax for getting the image from the dictionary.

The initializer UIImage imageNamed: is looking for the image in your main bundle, and since you've got it in a dictionary it won't find it. If you're actually storing a UIImage in the dictionary, why not just say...
PoctImage.image = PrdDetailsDic[#"PrdImg"];
I also suspect there's an issue with trying to save an image to the dictionary in the way you specify, for the same reason. It seems like that one should be...
#{
......
#"PrdImg":[image objectAtIndex:indexPath.row];
......
};

Related

App crashes when saving a dictionary object in Firestore

I was trying to save my quiz data into firestore like so
db.collection("Quizzes").addDocument(data: ["Author": userEmail,
"quizTitle": quizTitle,
"quizDescription": quizDescription,
"quizDictionary": quizDictionary]) { (error) in
if let e = error{
print("There was an erorr saving the data to the Firestore \(e)")
}
after clicking the button that would perform this block of code my app crashes and the following error is printed
020-09-19 17:17:46.367783+0800 Quizzler[18001:782362] -[__NSCFNumber length]: unrecognized selector sent to instance 0xc4cd28a1a98b5156
2020-09-19 17:17:46.469546+0800 Quizzler[18001:782362] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFNumber length]: unrecognized selector sent to instance 0xc4cd28a1a98b5156'
After doing some debugging, I found out that the "quizDictionary": quizDictionary part is what's causing the crash. Why is this happening? I thought that the method .addDocument(data: ) accepts [String: Any]?
If it is impossible to save Dictionary object into firestore, what are some workarounds for this?
If needed, this is my dictionary's declaration
dictionary declaration
Hey have a look here at the documentation for adding and managing data in fireStore :- https://cloud.google.com/firestore/docs/manage-data/add-data#swift.
Quick Overview line :- Using Map or Dictionary objects to represent your documents is often not very convenient, so Firestore supports writing documents with custom classes. Firestore converts the objects to supported data types.
Update :- As mentioned in comments it does not just convert any random object into a supported type. If you throw an NSImage at it, it won't know what to do with that. Or if the object contains some other random object it won't work. It has to contain supported types only. You can refer to this link to understand more about the supported data types.
You can only read and write supported data types with Firestore:
arrays
booleans
bytes
Firestore date objects
floating-point numbers
Firestore geographical objects
integers
maps (dictionaries, with string keys and values only of these supported types)
nil
Firestore references
strings
Therefore, you must convert your custom object to a format that is recognizable to Firestore which means replacing custom types with types that are primitive to the database.
https://firebase.google.com/docs/firestore/manage-data/data-types

Capturing Signature to Core Data

I have an app that creates a field for the user to sign their name. Upon clicking the save signature I want to create the image, save it as a png, and save it to core data.
"Pilot Signature" is it's own entity, with the property image.
- (IBAction)saveSignature:(id)sender) {
NSData *imageData = UIImagePNGRepresentation(mySignatureImage.image);
// Create an image object for the new image.
NSManagedObject *image =
[NSEntityDescription insertNewObjectForEntityForName:#"Image"
inManagedObjectContext:self.managedObjectContext];
self.mySignatureImage = imageData;
// Set the image for the image managed object.
[image setValue:image forKey:#"PilotSignatureImage"];
}
I get one error and one warning when I invoke saveSignature
expected methody body
And on self.MySignature.image line I get
incompatible types assigning to 'UIImageView" from "NSManagedObject"
I've looked similar posts on here about how to save image to core data, and I'm not having any luck figuring out what I did differently.
Any help would be appreciated
This code has serious problems. First, this line is not valid Objective-C:
- (IBAction)saveSignature:(id)sender) {
That last ) should not be there. This would probably explain an "expected method body" error, because you're confusing the compiler.
There's no self.MySignature.image line. I'm guessing that you mean the line that assigns a value to self.mySignatureImage.
self.mySignatureImage = imageData;
What that error is telling you is that self.mySignatureImage is a UIImageView but that imageData is an NSManagedObject. You can't assign one of those to the other, because they're completely unrelated things. Maybe you meant to type self.mySignature.image here instead, since you mentioned that in your question?
Finally this line doesn't make any sense:
[image setValue:image forKey:#"PilotSignatureImage"];
You're assigning image as an attribute of itself. That's got to be wrong. Maybe you want to be assigning imageData here?
I'm guessing that the last couple of lines should look like this, but it's impossible to be certain from the question:
self.mySignature.image = imageData;
[image setValue:imageData forKey:#"PilotSignatureImage"];
From this question and other related questions that you've asked, I think you would do well to review some of the basics of Objective-C. You seem to be having basic problems that aren't directly related to Core Data or image handling.

Couchbase iOS:How to convert a CBLDocument to NSDictionary?

What I am trying to do is convert a document to an dictionary and then iterate that dictionary to see what's inside, but I am having the following problem when trying to convert the document.
I have the following code:
CBLDocument *document = row.value;
NSDictionary *dict = document.properties;
the xCode is always complaining "[__NSCFDictionary currentRevision]: unrecognized selector sent to instance 0x90c4150" when running to the second statement.
I have also tried NSDictionary *dict = document.currentRevision.properties;
It's not working either.
Could anyone help me with that?
OK, it turns out row.value is not a document(is actually a dictionary), but nobody tells me that when I emit(#[somekey], doc); The doc (which is the value) is actually a dictionary instead of a document.
But weird though the compiler did not complain when assigning the dictionary to a CBLDocument.

Need help understanding a conditional crash when accessing my NSDictionary

I am keeping my data in a property called practiceRecords (an NSArray of dictionaries).
I check to see if the data already exists in the documents folder.
If yes, I load the data into self.practiceRecords.
If not, I build the array of dictionaries (using literal syntax), keeping this data in the self.practiceRecords property, and then write the data out to the documents folder.
(I am NOT reloading the data after writing it out)
As far as I am able to tell, there are no problems occurring during this process.
Then I have a step where I modify my data as follows ...
-(void)incNumberOfTriesFor:(NSString *)stringOfIndex {
if (self.practiceRecords)
{
int index = [stringOfIndex intValue];
int numberOfTries = [(NSNumber *)(self.practiceRecords[index][#"tries"]) intValue] + 1;
//CRASHING on this next line.
self.practiceRecords[index][#"tries"] = #(numberOfTries);
//message to helper method
[self writePracticeRecords];
}
}
So the first time through (when the array is built and written out) I get a crash at the indicated line.
The error is:
-[__NSDictionaryI setObject:forKeyedSubscript:]: unrecognized selector sent to instance
I quit the app, check the documents folder and see the data file written out with no issues.
I re-run the app, and then get no crash and the data file still looks great.
This is repeatable.
If the data file exists, no crash.
If the data first needs to be created, then a crash.
(In all cases, I manually look inside the resulting data file and see exactly what I expect to see - no issues there)
I'm not sure where to even begin squashing this bug, and would really like to understand the details of why this is happening.
Thanks very much for any help!
Just to recap the correct comments above:
-[__NSDictionaryI setObject:forKeyedSubscript:]: unrecognized selector sent to instance
NSDictionary does not implement any of the set... methods because it is immutable. You state that you're creating with literals syntax when the data is not found on disk. The literal syntax creates immutable containers
Instead, try...
// try to initialize from disk, but if not
// we can still use literal (immutable) syntax, but in a mutable container
self.practiceRecords = [NSMutableDictionary
dictionaryWithDictionary:#{ #"key" : #"value" }];

Saving custom class into Coredata

Hi i have used attribute type transformable and i have followed the procedure of archiving in one of the Forum .
But it says
CoreData: warning: no NSValueTransformer with class name 'SurveyResults' was found for attribute 'survey' on entity 'SurveyData'
2013-04-30 09:44:16.022 TestReachability[11641:c07] -[SurveyApplication encodeWithCoder:]: unrecognized selector sent to instance 0x845cb00
2013-04-30 09:44:16.023 TestReachability[11641:c07] * Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[SurveyApplication encodeWithCoder:]: unrecognized selector sent to instance 0x845cb00'
Here is my code
SurveyApplication *survey =[[SurveyApplication alloc]init];
survey.name=#"dheeraj";
survey.phone=#"573-356-2598";
NSManagedObject *aStory = [NSEntityDescription insertNewObjectForEntityForName:#"SurveyData"inManagedObjectContext:self.managedObjectContext];
[aStory setValue:survey forKey:#"survey"];
NSError *saveError = [[NSError alloc]init];
if (![self.managedObjectContext save:&saveError]) {
NSLog(#"%#",saveError);
}
SurveyAppplication object is my custom class and im trying to create an object and then store it core data .Could you please help me out.
Thanks
Dheeraj
It's not enough to make the attribute transformable, you also need to arrange for the transformation to happen. You can't just tell Core Data to transform any old object and expect it to know what to do. You have a couple of options:
Don't tell Core Data how to transform the data. In this case, Core Data will attempt to call encodeWithCoder: on your object to convert it to NSData. That's why you get the error that mentions this method-- it's trying to call the method on your class, but that method doesn't exist. In this case your class must conform to NSCoding for the transformation to occur.
Tell Core Data how to transform the data. In this case you create a subclass of NSValueTransformer that performs the transformation. You configure this on the attribute, either in the Core Data model editor or in code. In this case, you must have a custom transformer class that knows how to perform the transformation.

Resources