Setting NSBox's setContentView with NSImageView not working - objective-c

I have an NSBox set up in my main view that accepts drag and drop. We store the URL into str. We then load the image and add it to the content view of NSBox.
imageView = [[NSImageView alloc] initWithFrame:CGRectMake(0, 0, 390, 150)];
NSURL *imageURL = [NSURL URLWithString:str];
NSImage *image = [[NSImage alloc] initByReferencingURL:imageURL];
[imageView setImage:image];
[self setContentView:imageView];
However, this doesn't do anything. The image is not displayed in the nsbox.
Another oddity. At first I was trying to add the NSImageView via interface builder, but the only image container they had was IKImageView... Is this odd? Isn't NSImageView more ubiquitous? I mainly develop on iOS, so I have the version from the ios developer site.
Any thoughts?
Edit: I should also add, when I was using IKImageView in IB, the image would show up.
Edit2: I've also tried just taking the initWithFrame out and replacing it with just init, no go.

The problem was that I was using initByReferencingURL to get the NSImage. This doesn't work for local files, so instead I used initByReferencingFile and everything worked out well!

Related

Image is not visible on Watch App programatically

I have a very weird issue. I have a watch app and I am trying to display an image programatically. When I debug the code, I can see the image is correctly loaded but on simulator or real device I cannot see the image. I have added that image in both watch and extension bundle resources.
Firstly I use the code below to open the view.
NSArray *objects =[NSArray arrayWithObjects:#"MainPageView",#"InterfaceController",#"PoseImageView",nil];
[self presentControllerWithNames:objects contexts:nil];
Inside PoseImageView I am trying to display an image with:
UIImage *imgTest = [UIImage imageNamed:[NSString stringWithFormat:#"1.jpg", strAngle]]; // I can see 1.jpg in debugger
[_imgPose setImage: imgTest];
The image is not there on watch or simulator. I am wondering how to fix the issue.
Note: From assets its not loading as well.
Note2: I can only display the image if the view is initial view.
You are trying to use imageNamed for .jpg which will not work.
Try using
NSString *filePath = [[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:#"imageName"] ofType:#"jpg"];
UIImage *theImage = [UIImage imageWithContentsOfFile:filePath];

iOS 8.2 upgrade not show image in UIImageView, but only in app loaded through iTunes or TestFlight not Xcode

As weird as it sounds...
App loaded through test flight or iTunes does not show image on the screen, but it does when it is loaded through Xcode. I believe the UIImageView is there, and the UIImage is loaded.
App loaded through the Xcode works.
If the device is connected to the Xcode and debugging, device connected with USB cable. App works despite the load method...
Core data seems to works in all cases.
Any idea, somebody has seen something similar. I am quite lost.
Also if you have any tip on how to debug this on the device I will appreciate it.
The few code I think it is involve.
I am loading the UIImage from disk using
UIImage * previewImage = [UIImage imageWithContentsOfFile:path];
The image is located in Documents Directory fileName is the input
NSURL *documentsFolderURL = [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject];
NSString *documentsFolderPath = [documentsFolderURL path];
if (![_documentsDirectory isEqualToString:documentsFolderPath]) [self generateLocaFolderPath];
NSString *returnedPath = [_documentsDirectory stringByAppendingPathComponent:fileName];
return returnedPath;
and setting the image in the imageView with
previewView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, frameImageThumbnail.size.width, frameImageThumbnail.size.height)];
[previewView setImage:previewImage];
Everything else seems to work well...
My first question!!! Thanks for reading!
_ edit _
I had the strong believe now that the problem is not in the UIImage, I have use the same methods in other parts of the app and it is working fine.
But now I believe the problem is in the animation I perform to the UIView that contains the UIImageView.
[_view setFrame:frameImageThumbnail];
[_view setClipsToBounds:YES];
[_view setAutoresizesSubviews:YES];
[_image setAutoresizingMask:UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight];
[_view addSubview:_image];
[UIView animateWithDuration:0.4
delay:0.1
options:UIViewAnimationOptionCurveEaseInOut
animations:^(void){
CGRect destination1Frame = _view.frame;
//... some complicated calculations with the rect ...
[_view setFrame:destination1Frame];
[UIView transitionFromView:_customView toView:_view duration:0 options: UIViewAnimationOptionShowHideTransitionViews completion:^(BOOL finisehdCompletion){
//stop showing video in _customView
}];
}];
I am using NSAutolayout in the storyboard. Though my application only allows one landscape orientation. No idea if all this meshing up with the frames it is making.
dizzy to the poor auto layout guy...
Make sure you have / your image is set in Copy Bundle Resources. You can check if your image path is valid also. But I think the image is not set to copy at Build Phases.
I had the same problem.. Haven't find a correct solution.
But I deleted the image(check if it's deleted in ur Bundle resources), then put the image again, with ANOTHER name.
It worked for me. It's a temporary solution, idk if it's an apple bug or what.
Ok After more than 20 ipa files build and installed in my devices. In all this process to get rid of the problem I removed the _view from the storyboard, and initiallize the view programmatically. I remember that _view was originally set to (weak, nonatomic) property because was made with the storyboard, this super nice ctrl+.
That was I change I made, weak to Strong, and works now.
I don't know why it didn't work only when the program was install through iTunes with the ipa file or through the test flight app. That is still a mystery to me.

UIBarButtonItem with UIImage Always Tinted iOS 7

I'm trying to add a UIBarButtonItem containing a UIImage to a UIToolbar. The image keeps being tinted and I can't get it to show as the original colored image - all I want to do is display an image, verbatim, in a UIBarButtonItem! I'm following the directions in the iOS 7 transition guide to set the image rendering mode to UIImageRenderingModeAlwaysOriginal.
UIImage *image = [UIImage imageNamed:#"myImage.png"];
image = [image imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
UIBarButtonItem *ratingImage = [[UIBarButtonItem alloc] initWithImage:image style:UIBarButtonItemStyleBordered target:nil action:nil];
[toolbar setItems:[NSArray arrayWithObjects:ratingImage, nil] animated:YES];
One thing to note is that I set the tintColor for the main UIWindow of my app right when it loads...maybe this isn't important with regard to my issue, but thought I'd mention it.
I spent an evening trying to figure this out as well. You were very close to the solution.
The trick is to instantiate the UIImage with the rendering mode.
Instead of doing:
UIImage *image = [UIImage imageNamed:#"myImage.png"];
image = [image imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
do this:
UIImage *image = [[UIImage imageNamed:#"myImage.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
and it works!
In my case, I had dragged a Navigation bar to my viewcontroller in the IB, and added the BarButtonItem. But don't provide the item an image in the IB. Make an outlet and assign it the UIImage (like we created above) by doing this:
[myCustomBarButtonItem setImage:image];
Hope this works for you.
UIImageRenderingModeAlwaysOriginal can also be set by selecting the image in your Assets.xcassets "folder" in XCode and setting the "Render as" dropdown to "Original image".
For Swift 2.1+ it would look like this:
let image : UIImage? = UIImage(named:"myImage.png")!.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal)
UPDATED Swift 3
let image : UIImage? = UIImage(named:"myImage.png")!.withRenderingMode(.alwaysOriginal)
The accepted answer is fine but if you placed the UIBarButtonItem in a storyboard or xib then you can just:
Go to the Assets catalog where the image lives
Select the image
Go to the attributes inspector (cmd-opt-4)
Set "Render As" to "Original Image"
Only do this if you want all instances of this image to show up without tinting.
If you want it to work for versions of iOS less than v7, you might need to to this:
UIImage *image = [UIImage imageNamed:#"myImage.png"];
#try {
image = [image imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
} #catch (NSException *exception) {
}
Since imageWithRenderingMode: is an iOS 7 method, you'll get an exception if you try and use it with a lesser version.

WebView with NSShadow slowdown

I have a webview that shows some animated boxes, but it slows down and lags horribly when I put a drop shadow in the view. Without the shadow, the webview contents animate smoothly.
I achieved a similar effect as safari/chrome when you do a elastic scroll with the touchpad. Looks nice! But doesnt run as smooth as in my inspirations.
Here is the snippet of the shadow setting:
NSShadow *dropShadow = [[NSShadow alloc] init];
[dropShadow setShadowColor:[NSColor blackColor]];
[dropShadow setShadowOffset:NSMakeSize(0, 0)];
[dropShadow setShadowBlurRadius:5.0];
[webview setWantsLayer: YES];
[webview setShadow: dropShadow];
[dropShadow release];
--
My question is: How can I work around this problem?
You should not call setWantsLayer:YES on a WebView. Layer-backed web views are unsupported. This was actually pointed out in the 10.5 release notes and has not changed, unfortunately. You'll need to come up with an alternative solution.

adding UIImageView in UIScrollview with large image blocks thread

I am loading an image in a UIImageView which I then add to a UIScrollView.
The image is a local image and is about 5000 pixels in height.
The problem is that when I add the UIImageView to the UIScrollView the thread is blocked.
It is obvious because when I do this I can not scroll the UIScrollView till the image is displayed.
Here's the example.
UIScrollView *myscrollview = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 768, 1004)];
myscrollview.contentSize = CGSizeMake(7680, 1004);
myscrollview.pagingEnabled = TRUE;
[self.view addSubview:myscrollview];
NSString* str = [[NSBundle mainBundle] pathForResource:#"APPS.jpg" ofType:nil inDirectory:#""];
NSData *imageData = [NSData dataWithContentsOfFile:str];
UIImageView *singleImageView = [[UIImageView alloc] initWithImage:[UIImage imageWithData:imageData]];
//the line below is the blocking line
[scrollView addSubview:singleImageView];
It is the last line in the script that blocks the scroller. When I leave it out everything works perfect, except for the fact the image is not showing of course.
I seem to recall that using multithreading does not work on UIView operations so I guess that's out of the question ass well.
Thanks for your kind help.
If you're providing these large images, you should maybe check out CATiledLayer; there's a video of a good presentation on how to use this from WWDC 2010.
If these aren't your images, and you can't downsample them or break them into tiles, you can draw the image on a background thread. You may not draw to the screen graphics context on any main thread but the main thread, but that doesn't prevent you from drawing to a non-screen graphics context. On your background thread you can
create a drawing context with CGBitmapContextCreate
draw your image on it just as you would draw onto the screen in drawRect:
when you're done loading and drawing the image invoke your view's drawRect: method on the main thread using performSelectorOnMainThread:withObject:waitUntilDone:
In your view's drawRect: method, once you've fully drawn your image on the in-memory context, copy it to the screen using CGBitmapContextCreateImage and CGContextDrawImage.
This isn't trivial, you'll need to start your background thread at the right time, synchronize access to your images, etc. The CATiledLayer approach is almost certainly the better one if you can find a way to manipulate the images to make that work.
Why you want to load such huge image to memory at one time? split it into many small images and load/free it dynamically.
Try allocating the UIImageView without a UIImage, and add it as a sub-view to the UIScrollView.
Load the UIImage in a separate thread, and get the method running on the other thread to set the image property of the UIImageView when the image is loaded into memory. Also you'll probably encounter some memory problems as an image of this size loaded into a UIImage will probably be 30MB+
UIScrollView *myscrollview = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 768, 1004)];
myscrollview.contentSize = CGSizeMake(7680, 1004);
myscrollview.pagingEnabled = TRUE;
[self.view addSubview:myscrollview];
NSString* str = [[NSBundle mainBundle] pathForResource:#"APPS.jpg" ofType:nil inDirectory:#""];
UIImageView *singleImageView = [[UIImageView alloc] init];
[scrollView addSubview:singleImageView];
//Then fire off a method on another thread to load the UIImage and set the image
//property of the UIImageView.
Just keep an eye on memory and beware of using convenience constructors with UIImage (or any object that could end up being huge)
Also where are you currently running this code?