setContentMode: for HJManagedImageV is not working for iOS - image

I am using a third party lib, HJCacheClasses, for loading images asynchronously. Here is the code for the same. It's pretty much straightforward:
NSMutableString *url = #"my url";
HJManagedImageV *asyncImageView = [[HJManagedImageV alloc] initWithFrame:frame];
[asyncImageView setBackgroundColor:[UIColor grayColor]];
[asyncImageView showLoadingWheel];
[asyncImageView setContentMode:UIViewContentModeScaleAspectFill];
[asyncImageView.imageView setContentMode:UIViewContentModeScaleAspectFill];
asyncImageView.url = [NSURL URLWithString:url];
[self.imageManager manage:asyncImageView];
[the_pScrollView addSubview:asyncImageView];
Everything works fine except that the image is centered and it not getting stretched/fitted according to the size of the view (which is of size of full screen). I know the image is small but I need to make it fit in the view to fill the view. but none of the setContentMode are working.

If you have a look at the source code of the HJManagedImageV class, you will notice the author hardcoded the imageView of the HJManagedImageV instance to always be aspect fit. So by default you will never get aspect fill content mode working.
There's an easy way to achieve this though, right after you set the url property of the managed image view, add also a callback to your own class, like so:
myImageView.callbackOnSetImage = (id)self;
And in the callback function set the desired contentMode for the imageView, just like this. This is tested and works for me. Good luck
-(void) managedImageSet:(HJManagedImageV*)mi
{
mi.imageView.contentMode = UIViewContentModeScaleAspectFill;
}

I found the answer myself which I feel is more simpler and strightforward. As Ican mentioned,in the source code of the HJManagedImageV class,the author hardcoded the imageView of the HJManagedImageV instance to always be aspect fit. So I just commented that code... For people who like to do the same follow the following steps
Go to HJManagedImageV.m file
search for a method named
-(void) setImage:(UIImage*)theImage
comment the line
imageView.contentMode = ...
or set the content mode you want to for the imageView. Note: This will set the content mode for all the imageViews using this class. So I prefer to go with first option.
In the code where you are using this class, set the content mode for the HJManagedImageV.

Related

How to bind NSImageView by path?

I have a program with tableview/details of core data object.
One of the attributes is an image (it only appears in the details).
I think it's best to save just the path to the image file, rather than the image itself (it seems to me that core data files with images are much bigger than the images, even if there is little more than that...).
Since it is supposed that the user drags the image to the imagewell, I thought that it would be appropriate to bind the imagewell to the array controller (AC) using "Value path" (AC.selection.image).
However, this doesn't do anything (the imagewell accepts the image dragged there, but it keeps there when we change selection).
It seems likely to me that I must implement some "Value Transformer", but not the ones which are available (NSUnarchiveFromData and NSKeyedUnarchiveFromData), because I tried those already...
Am I right in this supposition? And if so, what value transformer would that be? Is it something that I'll have to define? Or is this altogether impossible?
Maybe I should add that I'm still using OSX 10.6, and thus some hypothesis seem to be ruled out...
Thanks
Yes. It is possible.
select the image view
Bind it to your object controller. In that value path field, you have to set the path value as string property
From my experience, the "value path" and "value URL" bindings only work one-way, as in, you can specify the image view's contents with them, but you can't extract the path/URL from a dragged image. The documentation says that the "binding is read-only", which is more than a little ambiguous, but I believe this is what it refers to.
I ended up just using a text box with a path inside, a "Choose…" button, and the image view as merely a preview. If you really want the "Image Well" functionality, however, I'd recommend something like KSImageView (GitHub gist) that grabs the path out of the NSPasteboard and stores/rebroadcasts it. Here's the main method for that functionality (after inheriting from NSImageView):
- (void)concludeDragOperation:(id <NSDraggingInfo>)sender {
NSPasteboard *pboard = sender.draggingPasteboard;
NSString *plist = [pboard stringForType:NSFilenamesPboardType];
if (!plist) return;
NSArray<NSString*> *files =
[NSPropertyListSerialization propertyListWithData:[plist dataUsingEncoding:NSUTF8StringEncoding]
options:NSPropertyListImmutable
format:NULL
error:NULL];
if (files.count == 0) return;
NSDictionary *userInfo = #{#"imagePath": files[0]};
[[NSNotificationCenter defaultCenter] postNotificationName:#"KSImageDroppedNotification"
object:self
userInfo:userInfo];
}

UIImageView not updating image when displayed in a UIScrollView

I am new to Xcode and objective C so the problem I have hit may be a simple one but I haven't been able to find an answer yet so thanks for any help you can give.
I am trying to write a simple app that takes a picture from the camera and displays it in a UIImageView. This all works fine if the UIImageView control is just placed in the UIView control (using the interface builder). The code I use to set the image to the control is
[self.imageViewOSFCorner setImage:image];
However I need to display several images an wanted the user to be able to scroll up and down the page. So I created a UIScrollView in the interface builder that is the size I need and placed all the controls in that. Then in the viewDidLoad method I placed the following code to display the UIScrollView
self.scrollView.contentSize = self.scrollView.frame.size;
self.scrollView.frame = self.view.frame;
[self.view addSubview:self.scrollView];
This works in that you can now scroll the page and see all of the UIImageView controls, and when I press the button it launches the camera, but now after taking the picture it doesn't display the image into the UIImageView. If I take the UIScrollView away it works again (but obviously doesn't scroll) - I am guessing I am doing something silly, is there a function I need to call to update the UIScrollView?
As an aside I originally tried to use the code
self.imageViewOSFCorner.image = image;
to display the image but this wouldn't display the image and had to use
[self.imageViewOSFCorner setImage:image];
can someone tell me why the first method didn't work?
Thanks
Will
May be useful:
self.scrollView.frame = CGRectMake(0, 0, self.view.frame.size.width*num,view.frame.size.hight );
num is your number of all image
Ok it is working now, I must have done something stupid when creating the controls into the UIScrollView since I have started from scratch again in the interface builder and now it all works.

UIView: hide drawing and show it again

I create a view at run time and I use it's drawRect: to draw a figure on it. In the next step I add a sublayer with contents of an image to the layer of the view and then I show it. It works. But the figure on the view is still shown below the image. With view.layer.contents = nil; before creating the sublayer, the figure on the view is removed. But I need to show it again. With [view setNeedsDisplay]; (calling drawRect:) I can draw it again.
Is there any (easier) way to hide (or cover) this figure and show it again (without removing and redrawing it)? Thanks.
Edit:
I can do this: layer.backgroundColor = [UIColor blackColor].CGColor; the figure is covered. But the backgroundColor must be transparent. Thanks very much for another ideas.
You can use:
view.layer.hidden = YES;
If you only want to hide and show a sublayer, you can keep hold of a reference to that sublayer, or find it by looking in view.layer.sublayers.

My DetailViewController isn't recognizing the '-configureView'

I had a problem with the splitView so I decided to make my own version of it using a ViewBasedApplication. The view is set up to be like a splitView, but instead of having the static table when you flip it to landscape it will have the popover control like it normally does in the portrait mode.
I have everything set up, but I'm encountering an error when I select an item from the popover table. It isn't updating the view to go to the appropriate page that was selected in the table...the code for the setDetailItem in the ViewController.m file looks like this:
-(void)setDetailTime:(id)newDetailItem{
if(detailitem != newDetailItem) {
[detailitem release];
detailItem = [newDetailItem retain[;
[self configureView];
...
}
The problem is in the [sef configureView] line. it says that "PDFViewController" may not respond to '-configureView'. Im assuming this is because '-configureView' is specific to only the SplitView. Any ideas of a way to workaround this issue?
Thanks!
Your class PDFViewController does not have a configureView method, which is why you're getting the warning (and why you're not seeing anything happen).
You probably need to instead call [self.view setNeedsDisplay] which basically tells the view to redraw itself. I don't know if this will work for you, because I don't know how the rest of your class is written.

NSImageView fails to setImage?

I'm trying to create a simple ImageView that just loads an image from disk, but this isn't working (I put it in my applicationDidFinishLoading):
NSString *file = [#"~/update.png" stringByStandardizingPath];
NSImage *image = [[NSImage alloc] initWithContentsOfFile:file];
[wView setImage:image];
Apparently the image for the NSImageView isn't being set, because if I NSLog [wView image], I get (null). I'm pretty sure I'm making a beginner mistake here, but what?
The file and image load fine; if I draw the image to an NSView it shows up fine. wView isn't nil, and I tried making another project with the default window's view a custom NSImageView subclass whose initWithFrame: calls setImage. Still nothing.
(OP here with an actual account)
So it turns out that setImage doesn't work on an NSImageView that's explicitly constructed in IB, if you get what I mean. I made an NSView subclass that constructs an NSImageView inside it and calls setImage, and that worked fine. Really annoying.
Most likely, wView is nil. The most common reason for this is when a class that's instantiated in the nib with a connection to a view also gets instantiated a second time in code.

Resources