Ok guys, I am fairly new to objective C. I have an app that downloads a list of images from my web server and displays the appropriate ones in a UIImage view with swipe gestures to go forward or backwards for the next/previous pic to be displayed. Current naming format for the pictures is like this:
uploaded_141_admin1.png
uploaded_141_admin2.png
uploaded_141_interior1.png
uploaded_141_interior2.png
uploaded_141_exterior1.png
The current code loads every picture into the view that has 141 in the middle part of the filename (or whatever record the user in on... 141 is variable in this instance, just showing here for an example of the format). The problem is, there seems to be no rhyme or reason as to what order they are displayed in. I would like it to use the last part of the filename to sort alphabetically (or even the whole filename, as it would achieve the same result). In the example above, it would display the downloaded pics in the following order when swiping through the uiimageiew:
uploaded_141_admin1.png
uploaded_141_admin2.png
uploaded_141_exterior1.png
uploaded_141_interior1.png
uploaded_141_interior2.png
I've search and can't find what I am looking for (maybe because I'm using the wrong search criteria). Here is my existing code that downloads and displays the images in the UIImageView. I assume the "sort" code would go in here somewhere:
-(void)downloadPictures:(NSArray *)picPaths {
ELog(#"Downloading pictures: %#",picPaths);
// wait indicator
[[WaitingView sharedInstance] setMessage:LocStr(#"Loading pictures... The more pictures there are, the longer this will take. Please be patient.")];
[[WaitingView sharedInstance] showIndicator:YES];
[[WaitingView sharedInstance] displayOn:[self view]];
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES];
// queue download operation
NSOperationQueue *queue = [[NSOperationQueue alloc] init];
NSInvocationOperation *downloadOp = [[NSInvocationOperation alloc] initWithTarget:self selector:#selector(downloadOperation:) object:picPaths];
[queue addOperation:downloadOp];
}
-(void)downloadOperation:(NSArray *)picPaths {
NSMutableArray *allPictures = [[NSMutableArray alloc] init];
for(NSString *path in picPaths) {
NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:#"http://%#%#/%#/%#",SERVER_ADDRESS,SERVER_PORT,SERVER_PHOTOS,path]];
NSData *picData = [NSData dataWithContentsOfURL:url];
if(picData!=nil) {
UIImage *img = [UIImage imageWithData:picData];
if(img!=nil) {
[allPictures addObject:img];
} else {
ELog(#"Failed to convert data to image from url %#",url);
}
} else {
ELog(#"Failed to download image from url %#",url);
}
}
[[WaitingView sharedInstance] performSelectorOnMainThread:#selector(remove) withObject:nil waitUntilDone:NO];
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];
self.pictures=allPictures;
if([self.pictures count]==0) {
[self performSelectorOnMainThread:#selector(downloadErrorMessage) withObject:nil waitUntilDone:NO];
} else {
self.currentIndex=0;
[self performSelectorOnMainThread:#selector(showPicture) withObject:nil waitUntilDone:NO];
}
}
-(void)downloadErrorMessage {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Oooops!" message:LocStr(#"Pictures download failed") delegate:nil cancelButtonTitle:LocStr(#"Close") otherButtonTitles:nil];
[alert show];
[alert release];
[self goBack];
}
-(void)showPicture {
UIImage *image = [self.pictures objectAtIndex:self.currentIndex];
ELog(#"Now displaying image with index %d: %#",self.currentIndex,image);
self.picture.image=image;
[self.picture setNeedsLayout];
}
In your downloadPictures: method you should sort your picPaths array to be the order you want the images before you start the download operation. You can do this by creating a new sorted array using the NSArray method sortedArrayUsingSelector:. Using caseInsensitiveCompare: as the selector for the sort will order the NSStrings in the array alphabetically.
NSArray *sortedPicPaths = [picPaths sortedArrayUsingSelector:#selector(caseInsensitiveCompare:)];
Then when you init your NSInvocationOperation, pass the sorted array as the object.
Related
I am creating a student index app, in which you can save names, pictures and roles of students. Everything works just fine, but when I created a certain amount of students (with pictures) while app-testing, the app runs extremely slow. Please find the corresponding methods enclosed. Is there any way to compress the size of the Image Data?
- (IBAction)cameraButtonPressed:(id)sender
{
if (! [UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera] ) {
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:#"Error" message:#"no access to camera" delegate:self cancelButtonTitle:#"Ok" otherButtonTitles: nil];
[alert show];
return;
}
UIImagePickerController *controller = [[UIImagePickerController alloc] init];
controller.delegate = self;
controller.sourceType = UIImagePickerControllerSourceTypeCamera;
[self presentViewController:controller animated:YES completion:nil];
}
- (void) imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)image editingInfo:(NSDictionary *)editingInfo
{
[picker dismissViewControllerAnimated:YES completion:nil];
_imageView.image = image;
_imageView.contentMode = UIViewContentModeScaleAspectFit;
}
- (IBAction)save:(id)sender
{
Student *student = [NSEntityDescription insertNewObjectForEntityForName:#"Student"
inManagedObjectContext:self.managedObjectContext];
self.student.picdata = UIImagePNGRepresentation(_imageView.image);
[self.managedObjectContext save:nil];
[self.delegate DetailStudentSavePressed:self];
}
You're already compressing the image. When you call UIImagePNGRepresentation, you get a PNG-style compressed image file.
You didn't post the details of your data model, but at a minimum, make sure that the picdata attribute is configured to use external storage in the model editor. Do that first.
If that doesn't help, there are other approaches to reducing the impact of binary blobs on Core Data. But those are not the next step. You specifically mention slowness rather than memory problems, and problems with Core Data and images are far more likely to cause memory issues. Rather than worry about image handling when you have a speed problem, use Instruments to profile your app. You'll find out exactly where it's slowing down.
In my app, the user can save an image to their documents directory. At launch, I grab the image, add a border, and put it into a UIImageview like this....
NSArray *sysPaths = NSSearchPathForDirectoriesInDomains( NSDocumentDirectory, NSUserDomainMask, YES );
NSString *docDirectory = [sysPaths objectAtIndex:0];
NSString *filePath = [NSString stringWithFormat:#"%#/ImageOne.jpg", docDirectory];
UIImage *unborderedImage = [[[UIImage alloc] initWithContentsOfFile:filePath] autorelease];
//image found....add border
UIImage *imageWithBorder = [self addBorderToImage:unborderedImage];
imageOneView.image = imageWithBorder;
Ideally, I like to check that the image is there first before adding a border. If not, load an "image not available" placeholder. Something like this:
NSArray *sysPaths = NSSearchPathForDirectoriesInDomains( NSDocumentDirectory, NSUserDomainMask, YES );
NSString *docDirectory = [sysPaths objectAtIndex:0];
NSString *filePath = [NSString stringWithFormat:#"%#/ImageOne.jpg", docDirectory];
UIImage *unborderedImage = [[[UIImage alloc] initWithContentsOfFile:filePath] autorelease];
NSError * error;
if (error != nil) {
//image found....add border
UIImage *imageWithBorder = [self addBorderToImage:unborderedImage];
imageOneView.image = imageWithBorder;
} else
//no image saved
[imageOneView setImage:[UIImage imageNamed:#"photoNotAvailable.png"]];
}
Of course, this doesn't work. I just can't seem to figure out how handle if "ImageOne.jpg" isn't found.
As it turns out, I need to do other things with the image elsewhere within the app. This would also depend on whether or not the user had saved an image or not. So in my method where the user can save the image, I send out a NSNotification that the image has changed. Then on my MainView, I look for the notification and key off that.
When saved:
[collectionOneUserDefinedDefaults setObject:#"image added" forKey:#"collectionOneImageAdded"];
[[NSNotificationCenter defaultCenter] postNotificationName:#"collectionOneImageChanged" object:self];
Then in my MainView I look for the notification:
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(updateCollectionOneImage) name:#"collectionOneImageChanged" object:nil];
- (void)updateCollectionOneImage {
//check if an image was ever saved, if so, replace the noPhotoAvailalble placeholder
NSUserDefaults *collectionOneUserDefinedDefaults = [NSUserDefaults standardUserDefaults];
NSString *collectionOneImageTextString = [collectionOneUserDefinedDefaults stringForKey:#"collectionOneImageAdded"];
if (collectionOneImageTextString == nil || [collectionOneImageTextString isEqualToString:#""]) {
[collectionOneImage setImage:[UIImage imageNamed:#"photoNotAvailable.png"]];
}
else {
//pull in collection one image from the Documents folder
NSArray *sysPaths = NSSearchPathForDirectoriesInDomains( NSDocumentDirectory, NSUserDomainMask, YES );
NSString *docDirectory = [sysPaths objectAtIndex:0];
NSString *filePath = [NSString stringWithFormat:#"%#/CollectionOneImage.jpg", docDirectory];
UIImage *unborderedImage = [[[UIImage alloc] initWithContentsOfFile:filePath] autorelease];
//image found....add border
UIImage *imageWithBorder = [self addBorderToImage:unborderedImage];
collectionOneImage.image = imageWithBorder;
}
}
It works perfectly. It in turn builds in the error handling. If the user saved and image, it gets loaded. If not, a placeholder image is loaded.
Your error handling here is doing nothing and is misguided. All you need do is simply check if unborderedImage is nil or not.
I've been struggling with this for quite a few days now; my app has a diagram with uitextfields to represent labelling of the picture. I would like to check the user input against a dictionary (for the answer) and if it is correct, increase the score by 1.
I had it working by 'hard coding' each of the textfield.text queries each with their own if statement, but I would like a better and more reusable way if possible?
I've tried this so far:
- (IBAction)checkAnswers:(UITextField *)textField
{
// array for each textfield
allTextfields = [[NSArray alloc] initWithObjects:eyepiece, objectiveLenses, focussingKnobs, stage, mirror, nil];
// array for each UIImageView
allTicks = [[NSArray alloc] initWithObjects:eyepieceTick, objectiveTick, focussingTick, stageTick, mirrorTick, nil];
UIImage *image = [UIImage imageNamed:#"Tick.png"];
for (textField in allTextfields) {
if ([textField.text isEqualToString:[[microscopeBrain.microscopeDictionary valueForKey:theTextfieldTag] valueForKey:#"Answer"]]) {
[[allTicks objectAtIndex:textField.tag] setImage:image];
x++;
textField.enabled = NO;
NSLog(#"%#", microscopeBrain.microscopeDictionary);
// NSLog(#"%#", [[microscopeBrain.microscopeDictionary valueForKey:theTextfieldTag] valueForKey:#"Answer"]);
}
finalMicroscopeScore = [[NSString alloc] initWithFormat:#"%i", x];
microscopeScoreLabel.text = [[NSString alloc] initWithFormat:#"%i", x];
}
}
The problem is that even if the answers are in the wrong textfield, as long as one is correct, they will all show up as right, which is kind of embarrassing!
Any help would be very much appreciated.
Try changing the valueForKey:theTextFieldTag to valueForKey:textField.tag and see if that helps. You don't show how you get the value for theTextFieldTag, so I'm not sure if that's the problem.
I'm relatively new to Core Data on iOS, but I think I've been getting better with it. I've been experiencing a bizarre crash, however, in one of my applications and have not been able to figure it out.
I have approximately 40 objects in Core Data, presented in a UITableView. When tapping on a cell, a UIActionSheet appears, presenting the user with a UIActionSheet with options related to the cell that was selected. So that I can reference the selected object, I declare an NSIndexPath in my header called "lastSelection" and do the following when the UIActionSheet is presented:
// Each cell has a tag based on its row number (i.e. first row has tag 0)
lastSelection = [NSIndexPath indexPathForRow:[sender tag] inSection:0];
NSManagedObject *managedObject = [self.fetchedResultsController objectAtIndexPath:lastSelection];
BOOL onDuty = [[managedObject valueForKey:#"onDuty"] boolValue];
UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:#"Status" delegate:self cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles:nil];
if(onDuty) {
[actionSheet addButtonWithTitle:#"Off Duty"];
} else {
[actionSheet addButtonWithTitle:#"On Duty"];
}
actionSheet.actionSheetStyle = UIActionSheetStyleBlackOpaque;
// Override the typical UIActionSheet behavior by presenting it overlapping the sender's frame. This makes it more clear which cell is selected.
CGRect senderFrame = [sender frame];
CGPoint point = CGPointMake(senderFrame.origin.x + (senderFrame.size.width / 2), senderFrame.origin.y + (senderFrame.size.height / 2));
CGRect popoverRect = CGRectMake(point.x, point.y, 1, 1);
[actionSheet showFromRect:popoverRect inView:[sender superview] animated:NO];
[actionSheet release];
When the UIActionSheet is dismissed with a button, the following code is called:
- (void)actionSheet:(UIActionSheet *)actionSheet willDismissWithButtonIndex:(NSInteger)buttonIndex {
// Set status based on UIActionSheet button pressed
if(buttonIndex == -1) {
return;
}
NSManagedObject *managedObject = [self.fetchedResultsController objectAtIndexPath:lastSelection];
if([actionSheet.title isEqualToString:#"Status"]) {
if([[actionSheet buttonTitleAtIndex:buttonIndex] isEqualToString:#"On Duty"]) {
[managedObject setValue:[NSNumber numberWithBool:YES] forKey:#"onDuty"];
[managedObject setValue:#"onDuty" forKey:#"status"];
} else {
[managedObject setValue:[NSNumber numberWithBool:NO] forKey:#"onDuty"];
[managedObject setValue:#"offDuty" forKey:#"status"];
}
}
NSError *error;
[self.managedObjectContext save:&error];
[tableView reloadData];
}
This might not be the most efficient code (sorry, I'm new!), but it does work. That is, for the first 25 items in the list. Selecting the 26th item or beyond, the UIActionSheet will appear, but if it is dismissed with a button, I get a variety of errors, including any one of the following:
[__NSCFArray section]: unrecognized selector sent to instance 0x4c6bf90
Program received signal: “EXC_BAD_ACCESS”
[_NSObjectID_48_0 section]: unrecognized selector sent to instance 0x4c54710
[__NSArrayM section]: unrecognized selector sent to instance 0x4c619a0
[NSComparisonPredicate section]: unrecognized selector sent to instance 0x6088790
[NSKeyPathExpression section]: unrecognized selector sent to instance 0x4c18950
If I comment out NSManagedObject *managedObject = [self.fetchedResultsController objectAtIndexPath:lastSelection]; it doesn't crash anymore, so I believe it has something do do with that. Can anyone offer any insight? Please let me know if I need to include any other information. Thanks!
EDIT: Interestingly, my fetchedResultsController code returns a different object every time. Is this expected, or could this be a cause of my issue? The code looks like this:
- (NSFetchedResultsController *)fetchedResultsController {
/*
Set up the fetched results controller.
*/
// Create the fetch request for the entity.
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
// Edit the entity name as appropriate.
NSEntityDescription *entity = [NSEntityDescription entityForName:#"Employee" inManagedObjectContext:self.managedObjectContext];
[fetchRequest setEntity:entity];
// Set the batch size to a suitable number.
[fetchRequest setFetchBatchSize:80];
// Edit the sort key as appropriate.
NSString *sortKey;
BOOL ascending;
if(sortControl.selectedSegmentIndex == 0) {
sortKey = #"startTime";
ascending = YES;
} else if(sortControl.selectedSegmentIndex == 1) {
sortKey = #"name";
ascending = YES;
} else {
sortKey = #"onDuty";
ascending = NO;
}
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:sortKey ascending:ascending];
NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor, nil];
[fetchRequest setSortDescriptors:sortDescriptors];
// Edit the section name key path and cache name if appropriate.
NSFetchedResultsController *aFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:self.managedObjectContext sectionNameKeyPath:nil cacheName:#"Root"];
aFetchedResultsController.delegate = self;
self.fetchedResultsController = aFetchedResultsController;
[aFetchedResultsController release];
[fetchRequest release];
[sortDescriptor release];
[sortDescriptors release];
NSError *error = nil;
if (![fetchedResultsController_ performFetch:&error]) {
/*
Replace this implementation with code to handle the error appropriately.
abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development. If it is not possible to recover from the error, display an alert panel that instructs the user to quit the application by pressing the Home button.
*/
//NSLog(#"Unresolved error %#, %#", error, [error userInfo]);
abort();
}
return fetchedResultsController_;
}
This happens when I set a breakpoint:
(gdb) po [self fetchedResultsController]
<NSFetchedResultsController: 0x61567c0>
(gdb) po [self fetchedResultsController]
<NSFetchedResultsController: 0x4c83630>
It's prob the case that self.fetchedResultsController is pointing to the wrong memory location. You will need to check if the object has been retained.
Figured it out! Looks like it was an issue with autoreleased objects.
When I turned on NSZombieEnabled, I got this:
*** -[NSIndexPath section]: message sent to deallocated instance 0xa674530
I simply changed lastSelection = [NSIndexPath indexPathForRow:[sender tag] inSection:0]; to lastSelection = [[NSIndexPath indexPathForRow:[sender tag] inSection:0] retain]; and that took care of it.
I'd like to adjust the NSApplicationIcon image that gets shown automatically in all alerts to be something different than what is in the app bundle.
I know that it's possible to set the dock icon with [NSApplication setApplicationIconImage:] -- but this only affects the dock, and nothing else.
I'm able to work around this issue some of the time: I have an NSAlert *, I can call setIcon: to display my alternate image.
Unfortunately, I have a lot of nibs that have NSImageView's with NSApplicationIcon, that I would like to affect, and it would be a hassle to create outlets and put in code to change the icon. And for any alerts that I'm bringing up with the BeginAlert... type calls (which don't give an NSAlert object to muck with), I'm completely out of luck.
Can anybody think of a reasonable way to globally (for the life of a running application) override the NSApplicationIcon that is used by AppKit, with my own image, so that I can get 100% of the alerts replaced (and make my code simpler)?
Swizzle the [NSImage imageNamed:] method? This method works at least on Snow Leopard, YMMV.
In an NSImage category:
#implementation NSImage (Magic)
+ (void)load {
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
// have to call imageNamed: once prior to swizzling to avoid infinite loop
[[NSApplication sharedApplication] applicationIconImage];
// swizzle!
NSError *error = nil;
if (![NSImage jr_swizzleClassMethod:#selector(imageNamed:) withClassMethod:#selector(_sensible_imageNamed:) error:&error])
NSLog(#"couldn't swizzle imageNamed: application icons will not update: %#", error);
[pool release];
}
+ (id)_sensible_imageNamed:(NSString *)name {
if ([name isEqualToString:#"NSApplicationIcon"])
return [[NSApplication sharedApplication] applicationIconImage];
return [self _sensible_imageNamed:name];
}
#end
With this hacked up (untested, just wrote it) jr_swizzleClassMethod:... implementation:
+ (BOOL)jr_swizzleClassMethod:(SEL)origSel_ withClassMethod:(SEL)altSel_ error:(NSError**)error_ {
#if OBJC_API_VERSION >= 2
Method origMethod = class_getClassMethod(self, origSel_);
if (!origMethod) {
SetNSError(error_, #"original method %# not found for class %#", NSStringFromSelector(origSel_), [self className]);
return NO;
}
Method altMethod = class_getClassMethod(self, altSel_);
if (!altMethod) {
SetNSError(error_, #"alternate method %# not found for class %#", NSStringFromSelector(altSel_), [self className]);
return NO;
}
id metaClass = objc_getMetaClass(class_getName(self));
class_addMethod(metaClass,
origSel_,
class_getMethodImplementation(metaClass, origSel_),
method_getTypeEncoding(origMethod));
class_addMethod(metaClass,
altSel_,
class_getMethodImplementation(metaClass, altSel_),
method_getTypeEncoding(altMethod));
method_exchangeImplementations(class_getClassMethod(self, origSel_), class_getClassMethod(self, altSel_));
return YES;
#else
assert(0);
return NO;
#endif
}
Then, this method to illustrate the point:
- (void)doMagic:(id)sender {
static int i = 0;
i = (i+1) % 2;
if (i)
[[NSApplication sharedApplication] setApplicationIconImage:[NSImage imageNamed:NSImageNameBonjour]];
else
[[NSApplication sharedApplication] setApplicationIconImage:[NSImage imageNamed:NSImageNameDotMac]];
// any pre-populated image views have to be set to nil first, otherwise their icon won't change
// [imageView setImage:nil];
// [imageView setImage:[NSImage imageNamed:NSImageNameApplicationIcon]];
NSAlert *alert = [[[NSAlert alloc] init] autorelease];
[alert setMessageText:#"Shazam!"];
[alert runModal];
}
A couple of caveats:
Any image view already created must have setImage: called twice, as seen above to register the image changing. Don't know why.
There may be a better way to force the initial imageNamed: call with #"NSApplicationIcon" than how I've done it.
Try [myImage setName:#"NSApplicationIcon"] (after setting it as the application icon image in NSApp).
Note: On 10.6 and later, you can and should use NSImageNameApplicationIcon instead of the string literal #"NSApplicationIcon".