NSInvalidArgumentException exception when adding image to imageView iOS - ios

.H files
#property (strong, nonatomic) LAClaimReport *claimReport;
.M file
#interface LACreateReportViewController ()
{
NSArray *_thumbnails;
LAPhotoThumb *_lastSelectedPhoto;
}
_thumbnails = _claimReport.photos.allObjects;
if (_thumbnails.count >0)
{
_photosCaption.textColor = [UIColor colorWithRed:0/255.0 green:46/255.0 blue:91/255.0 alpha:1];
UIImageView *image = [[UIImageView alloc] initWithFrame:CGRectMake(30, 1838, 300, 600)];
NSLog(#"image:%#" , _thumbnails[0]);
[image setImage:(UIImage *) _thumbnails[0]]; // exception here.
}
NSInvalidArgumentException', reason: '-[LAPhoto size]: unrecognized selector sent to instance 0xa76b720'
Photos is a column in the class file of LAClaimReport. What am i missing in regards to size here? pls guide.

_thumbnails doesn't contain UIImage instances, it contains some other type of object. Enough information isn't included to tell what kind of objects it contains however.
size is a method on UIImage that is used by the image view to determine how to display the image, and whatever object is actually in _thumbnails doesn't implement that method.
Your NSLog(#"image:%#" , _thumbnails[0]); should list the class type that you're trying to use as an image.

make sure that you have UIImage class objects in _thumbnails Array, I Think you have LAPhoto type objects in that array
id arrContent=_thumbnails[0];
if ([arrContent isKindOfClass:[LAPhoto Class]])
{
//read LAPhoto class and find a way to get the UIImage from LAPhoto Object
}
else if ([arrContent isKindOfClass:[UIImage class]])
{
[image setImage:(UIImage *) _thumbnails[0]];
}

Related

Inability to create my UIImageView classes from string

I am creating subclasses of UIImageView class, something like LittleImageView, RedImageView, etc.
These subclasses have this convenience method for the creation of specific images:
+ (UIImageView *)novo {
UIImageView *newImage = [[super alloc] initWithImage:...
// do stuff
return newImage;
}
When I try to create such classes using this new command by string
id newObject = [NSClassFromString(nameOfClass) novo];
I get a crash with "unrecognized selector sent to class". Apparently objective-c is trying to do a [UIImageView novo] instead of doing a [RedImageView novo], for instance.
Any ideas how to solve this?
I can't reproduce your experience exactly, but there are a few changes you should consider: (1) declare the constructor as returning the derived type, (2) declare the local variable as the derived type, (3) used the derived class to alloc (self, not super)...
// in MyImageView.h
+ (instancetype)myImageView:(UIImage *)image;
// in MyImageView.m
+ (instancetype)myImageView:(UIImage *)image {
MyImageView *miv = [[self alloc] initWithImage:image];
// ...
return miv;
}
Now you can use elsewhere without the sketchy use of id in your local variable declaration. It looks like this, and in my test, generates instances of the correct type...
MyImageView *firstTry = [MyImageView myImageView:nil];
MyImageView *secondTry = [NSClassFromString(#"MyImageView") myImageView:nil];
NSLog(#"%#, %#", firstTry, secondTry);

Unable to set UIImage, always returns nil

This is probably a ridiculous question but I just can't figure it out for the life of me. I have a class which subclasses NSObject which has a property called image setup, like this:
#property (strong, nonatomic) UIImage *image;
Somewhere else I initialize the class and attempt to set the image in the class, like this:
UIImage *image = [UIImage imageNamed:#"Test"];
Class *item = [[Class alloc] init];
item.image = image;
But when I try to call item.image it is always nil.
The image called Test is in the project and is used elsewhere so I know it has access to the image, but for some reason it isn't being copied to the instance of my class. I have put NSLog messages and breakpoints directly after setting the image which have shown it to be nil as well as using commands via the debugger which have also shown the image in the class to be nil while the UIImage I create to copy is not nil.
I know I am doing something wrong but I have searched and can't figure out what it is.
Thanks for looking, any help is much appreciated.
edit:
I am trying to access the image from a method inside of the class.
- (void)save {
if (self.image != nil) {
// do some stuff
} else {
NSLog(#"image is nil");
}
}
And it is always coming up nil.
edit 2:
Thanks again everyone. I figured out what the problem was. I was overriding the setters/getters which was causing the issue.
Is it a .png file? Try doing it like this:
UIImage *image = [UIImage imageNamed:#"Test.png"];

Getting string value from nssarray

I define an array in the 'interface' section of the view controller thus:
#property (strong, nonatomic) NSArray *images;
I load it in viewDidLoad:
self.images = #[[UIImage imageNamed:#"sophia1_1x"],
[UIImage imageNamed:#"sophia2_1x"],
[UIImage imageNamed:#"sophia3_1x"],
[UIImage imageNamed:#"sophia4_1x"],
[UIImage imageNamed:#"sophia5_1x"],
[UIImage imageNamed:#"sophia6_1x"]];
Then I want to set the image in a UIImage from the array. I can hard code the name of the image, and it works fine:
self.currentImage.image = [UIImage imageNamed:#"sophia4_1x.png"];
But if I try to get the value from the array:
self.currentImage.image = [UIImage imageNamed:self.images[row]];
I get "unrecognized selector sent to instance"
and I just...don't........understand!
Your array contains UIImage objects, not strings, so you can't pass self.images[row] to the imageNamed: method. To get an image, you just need to access the row you want in the array,
self.currentImage.image = self.images[row];
Did you mean to save an array of strings, and then use a string to load an image?
If so, your code would look like this:
#property (strong, nonatomic) NSArray *imageNames;
And the code to load it:
self.imageNames = #[
#"sophia2_1x",
#"sophia3_1x",
#"sophia4_1x",
#"sophia5_1x",
#"sophia6_1x"
];
Then your code to load an image would work:
self.currentImage.image = [UIImage imageNamed:self.imageNames[row]];
If you really wanted an array of images then relmar's solution would give you that.

CUICatalog: Invalid asset name supplied: , or invalid scale factor: 2.000000

I have a UiVIewController in which i have dragged a table view and put all the needed connections like delegate and data source and it works just fine, everything is great. I tried to set a background to this table view , and the i got this weird error
CUICatalog: Invalid asset name supplied: , or invalid scale factor: 2.000000
I tried to set the background using this method :
UIImageView *tempImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"mypredictions_bg.png"]];
[tempImageView setFrame:self.tableView.frame];
self.tableView.backgroundView = tempImageView;
What am i missing ? I checked the name of the picture is correct
In My case I made category for UIImageView and UITextfied,
In That, sometimes I don't need an image, that time I supplied #"" (i.e. Null string), it occurs problem, after some R&D I supply just "nil" instead of #"", it solves my warning, so maybe this type of warning occurs while not get proper image.
I made a debug class which swizzles imageNamed so that you can get a debug trace on where this is happening.
You need to install JRSwizzle to use it.
https://github.com/rentzsch/jrswizzle
#import "UIImageDebugger.h"
#import "JRSwizzle.h"
#implementation UIImageDebugger
+ (void)startDebugging
{
static dispatch_once_t once;
dispatch_once(&once, ^{
NSError *error=NULL;
[UIImage jr_swizzleClassMethod:#selector(imageNamed:)
withClassMethod:#selector(hs_xxz_imageNamed:)
error:&error];
if (error)
{
NSLog(#"error setting up UIImageDebugger : %#",error);
}
else
{
NSLog(#"!!!!!!!!!!!!!!!!!!!! UIImage swizzle in effect - take this out for release!!");
}
});
}
#end
#interface UIImage (UIViewDebugger)
+ (UIImage*)hs_xxz_imageNamed:(NSString *)name;
#end
#implementation UIImage (UIViewDebugger)
+ (UIImage*)hs_xxz_imageNamed:(NSString *)name
{
if (!name)
{
NSLog(#"null image name at \n%#",[NSThread callStackSymbols]);
}
UIImage *image=[self hs_xxz_imageNamed:name];
if (!image)
{
NSLog(#"failed to make image at \n%#",[NSThread callStackSymbols]);
}
return image;
}
#end
If your image type is png, you don't have to replace it end of the image filename because default XCode image type is already png. Try this;
UIImageView *tempImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:[NSString stringWithFormat:#"mypredictions_bg"]]];
In my case, was directing to the wrong viewController.
I was directing to the viewController that handled the content of a page based ViewController, instead of linking to the viewController in which the pageContentViewController as well as the PageViewController were linked. Hope this helps.

How to get a UIImage's image as an NSString?

I need to get the value of a UIImage's image as a string.
Previously I was doing:
if(images[imageNo].image == [UIImage imageNamed:#"image1.png"])
{
But now I have a lot more conditions so I would like to use a switch, doing:
switch(images[imageNo].image)
{
case #"image1.png":
break;
}
Is there a way to achieve this? Thanks!
Mmm I am not sure what you want to accomplish here but using [UIImage imageNamed:#"image1.png"], is definitely a very bad way to do the comparison as you are creating a new UIImage object on the fly for no reason at all, furthermore you are using the == operator when you should be using isEqual since they are objects.
I believe what you want to do is convert it to a base 64 string perhaps?. If so you an use this:
NSData *imageData = UIImageJPEGRepresentation(myImage, 1.0);
NSString *encodedString = [imageData base64Encoding];
The answer provided by H2C03 is appropriate. Use a dictionary to associate the names.
However, an alternative is using associative objects. Note that there is a cost in both space and time using associative objects, but they are convenient for just this type of case (adding a category-property).
#interface UIImage (MyImageLabel)
#property (nonatomic, copy) NSString *label;
#end
#import <objc/runtime.h>
#implementation UIImage (MyImageLabel)
static char const kImageLabelKey[1];
- (NSString*)label {
return objc_getAssociatedObject(self, kImageLabelKey);
}
- (void)setLabel:(NSString *)label {
objc_setAssociatedObject(self, kImageLabelKey, label, OBJC_ASSOCIATION_COPY_NONATOMIC);
}
#end
Now, you can have a label property on your UIImage instances, like so:
myImage.label = someLabelStringCouldEvenBeFilename;
and
if ([myImage.label isEqualToString:someString]) {
}
The typical caveats apply regarding anything in a category. Most will encourage you to use a unique prefix or postfix to differentiate your category methods from potential future Apple names.
Note, that you could add another method to the category, like this...
+ (UIImage)myImageNamed:(NSString*)name {
id result = [self imageNamed:name];
[result setLabel:name];
return result;
}
and now you automatically set the label.
Of course, you could do this as a subclass, if you will always be creating your own image, and avoid the "nastiness" associated with associative objects (though all your images need to be MyUIImages).
#interface MyUIImage : UIImage
#property (nonatomic, copy) NSString *label;
#end
#implementation MyUIImage
// Now, you override imageNamed:
+ (UIImage*)imageNamed:(NSString*)name {
UIImage *image = [super imageNamed:name];
self.label = name;
return image;
}
#end
Both of your approaches are incorrect. The first one relies on caching (which is not documented), meanwhile the second is a syntax error - the 'case' keywords expect a compile-time constant expression.
As far as I know, there's no such a method in the iOS SDK that would return an image's filename - simply because it's not a property of the image (what would it return for a programmatically created image?). You should instead try to operate on an NSDictionary, storing the UIImage objects associated with the filenames as keys, and comparing the keys using isEqualToString:.
So you should create your images only once, and store them in a dictionary:
// assuming `images` is an already created instance variable of your class
[images setObject:[UIImage imageNamed:#"ImgOne.png"] forKey:#"ImgOne.png"];
// and so on, with every image you need
// then once you have to check against a file name, use:
UIImage *img = [images objectForKey:#"FileName.png"];
if ([someImage isEqual:img]) {
// you can now be sure that the image set to the object was once named "FileName"
}

Resources