It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 9 years ago.
I am getting locally saved image url and now i am trying to convert it to string. I know absoluteString is the method to convert url to string but in this case not working.
my url is like this (file://localhost/Users.......)
NSString * imagestring = [entryObject.imageurl absoluteString];
but its throw error unrecognized selector sent to instance 0xc36c410 . I dont know why its happening like this
here is my object class
#interface EntryObject : NSObject
#property (nonatomic, strong) NSString *descString;
#property (nonatomic, strong) UIImage *photoImage;
#property (nonatomic, strong) NSURL * imageurl;
#property (nonatomic, strong) NSString *dairyId;
#property (nonatomic, strong) NSString *eventId;
#property (nonatomic, strong) NSString *dateString;
#property (nonatomic, strong) NSString *monthwiseSeparate_String;
#property (nonatomic,strong) NSString *sortedString;
#property (nonatomic,strong) NSData * entryimagedata;
entryObject.imageurl = [NSURL fileURLWithPath:yourPath];
If you're getting unrecognized selector sent to instance error that means, that you've sent a message to a object, which does not know how to interpret this message. I.e., say, you have an NSString object and you're sending a absoluteString message to it, you'll get this error.
So the reason for that is definitely that even though imageurl is declared as NSURL you may(and probably accidentally did) assign object of some other class to it. Looks like you just have a NSString there, obtained from a database and to solve your problem you just don't need to transform to NSString as far as it's already a NSString. Post your code here where you set imageURL property, where you send absoluteString message. Also place a breakpoint just before the line where you send absoluteString to a imageURL and check the class of imageURL.
try this:
NSString * imagestring = (NSString *)[entryObject.imageurl absoluteString];
or
NSString * imagestring = (NSString *)[entryObject imageurl];
Related
I have an IOS app that is using RestKit to pull json formatted data from a server into a CoreData Entity. One of the attributes in the data is a URL for the image associated with the particular article. I'm trying to load that image to my collectionViewController. This is what I've been trying with no success.
From within
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
I am trying this
NSManagedObject *object = [self.fetchedResultsController objectAtIndexPath:indexPath];
NSURL *photoURL = [object valueForKey:#"imageUrl"];
NSData *photoData = [NSData dataWithContentsOfURL:photoURL];
cell.cellimg = [[UIImage alloc] initWithData:photoData];
[cell.title setText:[object valueForKey:#"title"]];
If I comment out the attempt to grab and load the image the view loads great with a default image and the titles of the articles received from the json data. Based on that I know everything is coming in correctly.
I have also determined that *photoData is being assigned the correct URL. But each time I encounter the NSData line the console prints out "-[__NSCFString isFileURL]: unrecognized selector sent to instance...."
I honestly don't know if this is even the correct way to do this or if it will even work. I am pretty new at this so any help would be great. Given that I am new some small code examples would really help as well as explaining the proper way to approach this.
Just in case here is the header file where I define *cellimg and *title
#import <UIKit/UIKit.h>
#interface GlobismNewsCollectionViewCell : UICollectionViewCell
#property (strong, nonatomic) IBOutlet UIImage *cellimg;
#property (weak, nonatomic) IBOutlet UILabel *title;
#property (weak, nonatomic) NSURL *imageUrl;
#end
I think you are using NSString as NSURL. So try to use this one...
NSURL *photoURL = [NSURL URLWithString:(NSString *) [object valueForKey:#"imageUrl"]];
Thanks to Dharmbir I had this issue wrapped up within 25 minutes of asking the question!
To clarify for any new guys like me I changed the line Dharmbir suggested so the block looks like this now
NSManagedObject *object = [self.fetchedResultsController objectAtIndexPath:indexPath];
NSURL *photoURL = [NSURL URLWithString:(NSString *) [object valueForKey:#"imageUrl"]];
NSData *photoData = [NSData dataWithContentsOfURL:photoURL];
[cell.cellimg setImage:[[UIImage alloc] initWithData:photoData]];
[cell.title setText:[object valueForKey:#"title"]];
Notice the 4th line in the block also changed from this
cell.cellimg = [[UIImage alloc] initWithData:photoData];
to this
[cell.cellimg setImage:[[UIImage alloc] initWithData:photoData]];
I also had to change my *cellimg property to this
#property (strong, nonatomic) IBOutlet UIImageView *cellimg;
from this
#property (strong, nonatomic) IBOutlet UIImage *cellimg;
I had to change to the UIImageView class because the cell in my storyboard is based on the UIImageView Class. Hope this helps a guy in need thanks again Dharmbir!
I have used the following code.
MainView.h:
NSString *sCopySource;
NSString *sFileSource;
// retain and copy used both for test proposes
#property (nonatomic, retain) NSString *sCopySource;
#property (nonatomic, copy) NSString *sFileSource;
MainView.m:
// Inside the first method:
sCopySource = [NSString stringWithFormat:#"%#%#", path1, filename];
sFileSource = [NSString stringWithFormat:#"%#%#", path2, filename];
// Inside the second method:
[[NSFileManager defaultManager] copyItemAtPath:sCopySource toPath:sFileSource error:&err];
And take error in the last line of the code by zombie-enabled objects sCopySource and sFileSource:
message sent to deallocated instance
Why? The properties marked as retain and copy. How to fix this?
Thanks a lot for the help!
P.S. Please don't answer to use ratain and release methods. They're extremely inconvenient.
You have defined the property, but you are writing directly to the instance variable.
If you want to use the retain/release logic in the property, you need to use:
self.sCopySource = [NSString stringWithFormat:#"%#%#", path1, filename];
self.sFileSource = [NSString stringWithFormat:#"%#%#", path2, filename];
That way, the methods that do the copy and retain are used.
Maybe a very basic problem, but i don't get it ...
I've written my own class for having Data as an Object in my iPhone app. The class is of Type NSObject. The Class has several NSString propertys and also three objects of other classes.
#interface vcardItem : NSObject {
vCardNitem *PersonName;
NSString *Phone;
NSString *PhotoURL;
vCardTELitem *PhoneData;
NSString *URL;
NSString *eMail;
vCardADRItem *Address;
NSString *Company;
NSString *Role;
int initialised;
}
#property (nonatomic, retain) vCardNitem *PersonName;
#property (nonatomic, retain) NSString *Phone;
#property (nonatomic, retain) NSString *PhotoURL;
#property (nonatomic, retain) vCardTELitem *PhoneData;
#property (nonatomic, retain) NSString *URL;
#property (nonatomic, retain) NSString *eMail;
#property (nonatomic, retain) vCardADRItem *Address;
#property (nonatomic, retain) NSString *Company;
#property (nonatomic, retain) NSString *Role;
#property int initialised;
-(id)initEmpty;
-(id)initWithPayload:(NSString *)payload;
-(void)sendToAddressBook;
-(void)debugVCard;
-(NSString *)getFirstName;
#end
I'm creating an object of vcardItem in a function call within my viewcontroller. The Data is filled and accessible. The function initWithPayload is called and all propertys are set.
Then i open another view for showing Details and call the function setVCard with my vcardItem Object (which is still completeley accessible):
VCardViewController *aVCardViewController = [[VCardViewController alloc] init];
[self presentModalViewController:aVCardViewController animated:YES];
[aVCardViewController setVCard:aVCard];
within the VCardViewController then, only the NSString propertys are accesible. As soon as i stry to access propertys of VCardNItem, vCardADRitem or vCardTELitem, i get an EXC_BAD_ACCESS Error.
Looks like the Objects within the vcardItem Object are not available !?
I've set the NSZombie... thing, my vcardItem Object is still living (No "Zombie"), but i cannot access the data.
any Idea? I'm a little bit stucked here....
Code Review did the job, after building in Xcode 4 against iOS 5 all is fine!
I am having a crash I do not understand. I am downloading a couple files from a remote server, and when a file has finished downloading, I am attempting to retrieve an object that was attached to the request object.
Tia for any help!
S.
Main Class
for (TrainingEntry* _entry in objects) {
HTTPRequest * request = [HTTPRequest requestWithURL:[NSURL URLWithString:[NSString stringWithFormat:#"%#/%#", [[ConfigLoader config] objectForKey:#"Server"], _entry.url]]];
[request setDownloadDestinationPath:[NSString stringWithFormat:#"%#/%#", documentsDirectoryPath, _entry.filename]];
[request setEntry:_entry];
[request setRaw:_entry.title];
TrainingEntry * test = (TrainingEntry*)[request _entry];
NSLog(#"title: %#", [test title]); //<= This works
NSLog(#"filename: %#", [test filename]); //<= This works
[[self networkQueue] addOperation:request];
}
// Start Queue
[[self networkQueue] go];
...
- (void)requestFinished:(HTTPRequest *)request {
if ([[self networkQueue] requestsCount] == 0) {
[self setNetworkQueue:nil];
}
NSLog(#"req: %#", [request raw]);
NSLog(#"title: %#", [[request entry] title]); // <= This works
NSLog(#"filename: %#", [[request entry] filename]); // <= This crashes
}
HTTPRequest Object
#interface HTTPRequest : ASIHTTPRequest {
}
#property (nonatomic, retain) TrainingEntry * entry;
#property (nonatomic, retain) NSString * raw;
#end
TrainingEntry Class
#interface TrainingEntry : NSObject {
NSString * title;
NSString * filename;
}
#property (nonatomic, retain) NSString * title;
#property (nonatomic, retain) NSString * filename;
Something that may be giving you trouble is not having copy in your property declaration. You should consider using copy for all objects that conform to the NSCopying protocol specification, especially NSString and other collection classes.
In the past, changing retain to copy for an NSString property solved a similar issue for me.
edit: some research
This post may help clarify why you should use copy over retain for immutable objects.
When a crash occurs, there will be a backtrace.
Post it.
Either your program will break in the debugger, and the call stack will be in the debugger UI (or you can type 'bt
With that, the cause of the crash is often quite obvious. Without that, we are left to critique the code.
If this crashes:
NSLog(#"filename: %#", [[request entry] filename]); // <= This crashes
It is either because request has been over-released and is no longer viable or because filename is returning a garbage object. Given that request worked previously, it is likely that filename has been over-released.
Where do you set filename?
Does anyone know how I should go about storing a UIImage with the version of ObjectiveRecord packaged with ObjectiveSync? (Note that the version of SQLPersistantObjects in ObjectiveRecord is older than the current version) At the moment, I am attempting to store the UIImage data as NSData:
#interface Picture : SQLitePersistentObject {
NSString *pictureId;
NSDate *updatedAt;
NSData *imageData;
UIImage *image;
}
#property (nonatomic, retain) NSString *pictureId;
#property (nonatomic, retain) NSData *imageData;
#property (nonatomic, retain) NSDate *updatedAt;
I can set the data OK, and store it in the DB, but when I select a record from the DB, I get the data as a NSCFString as opposed to NSData. Does anyone know why?
If you want to store the image in the sqlite database then, the image should be stored as BLOB type in the sqlite database