I created a framework using the Xcode 6 Cocoa Touch Framework target feature.
When the framework is used in another app running on iOS 8, it works perfectly fine, loading images, picking up nibs-storyboards etc..
When I run the same app on an iOS 7 device, the code is picked up, view controllers are showing up, images that are set in the interface builder for the nib are showing up. But the images that are being set through code are not showing up.
The image is present in the app's contents (when I do a show package contents).
But its not getting picked up. Please see my code.
NSString *bundlePath = [NSString stringWithFormat:#"%#/Frameworks/TestFramework.framework", [[NSBundle mainBundle] bundlePath]];
NSBundle *frameworkBundle = [NSBundle bundleWithPath:bundlePath];
[frameworkBundle load];
NSString *imagePath = [NSString stringWithFormat:#"%#/Pic2.JPG", bundlePath];
UIImage *image = [UIImage imageNamed:imagePath];
NSLog(#"FrameworkBundle:%#", frameworkBundle);
NSLog(#"BundlePath:%#", bundlePath);
NSLog(#"ImagePath:%#",imagePath);
NSLog(#"Image:%#",image);
self.imageView3.image = image;
This is the output
2014-11-19 11:48:01.324 TestApp[95937:60b] FrameworkBundle:NSBundle </Users/nbkqkka/Library/Developer/CoreSimulator/Devices/A64CFDA9-EB0E-4B65-B119-A213CD500217/data/Applications/C942AA07-E416-48B3-AE96-C853DB349B64/TestApp.app/Frameworks/TestFramework.framework> (loaded)
2014-11-19 11:48:01.324 TestApp[95937:60b] BundlePath:/Users/nbkqkka/Library/Developer/CoreSimulator/Devices/A64CFDA9-EB0E-4B65-B119-A213CD500217/data/Applications/C942AA07-E416-48B3-AE96-C853DB349B64/TestApp.app/Frameworks/TestFramework.framework
2014-11-19 11:48:01.325 TestApp[95937:60b] ImagePath:/Users/nbkqkka/Library/Developer/CoreSimulator/Devices/A64CFDA9-EB0E-4B65-B119-A213CD500217/data/Applications/C942AA07-E416-48B3-AE96-C853DB349B64/TestApp.app/Frameworks/TestFramework.framework/Pic2.JPG
2014-11-19 11:48:01.474 TestApp[95937:60b] Image:(null)
The documentation for imageNamed says:
The name of the file. If this is the first time the image is being
loaded, the method looks for an image with the specified name in the
application’s main bundle.
It doesn't say anything about loading from a full path.
Can you use:
+ (UIImage *)imageWithContentsOfFile:(NSString *)path
instead?
Related
I am using MPMoviePlayerController to play video on iOS.
First time I hit the url and video gets downloaded to documents directory & played well.
Second time I check if the video is already downloaded or not.
If not then it goes to server & download it,
if yes then it should access it from documents directory and should play video.
but when I fetch video from documents directory then it shows the path well, along with video file name, but
it doesn't play the video.
Directly it moves to last view with blank white screen.
What I am doing wrong.
Thanks in advance.
Here is my code.
NSArray *arrayPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);
NSString *docDir = [arrayPaths objectAtIndex:0];
NSString* destinationDirectoryPath=docDir;
NSString *clientPathString = [[NSString alloc] initWithFormat:#"/user"];
destinationDirectoryPath = [destinationDirectoryPath stringByAppendingString:clientPathString];
destinationDirectoryPath = [destinationDirectoryPath stringByAppendingString:#"/video1/"];
NSString *filePath=[NSString stringWithFormat:#"%#",userVideoDTO.learnerid];//#"author";
filePath=[destinationDirectoryPath stringByAppendingString:[NSString stringWithFormat:#"%#.mp4",filePath]];
NSLog(#"FilePath new one :- %#",filePath);
self.url = [NSURL fileURLWithPath:filePath];
Then this self.url is passed to MPMoviePlayerController as
self.mp =[[MPMoviePlayerController alloc] initWithContentURL:self.url];
[self.view addSubview:mp.view];
[self.mp play];
What I am missing.
Can anyone let me know.
This same code is working fine on lower versions of iOS. like iOS7 and below.
not on iOS8.
Thanks in advance.
I have been having a similar issue, and it turned out that while in iOS7 and below the containing view controller hadn't had it's -viewWillDisappear:animated: called before the movie player was shown, whereas in iOS8 and going further, it does get called. I was releasing the player and unsubscribing from notifications in -viewWillDisappear:animated:, this time I had to move this code to dealloc.
I am using iOS 6's UIActivityViewController.
I would like to share an image that is not available locally on my iPhone, but that it is available on a remote URL.
NSString *textToShare = _eventoTitle;
UIImage *imageToShare = [UIImage imageNamed:_iconUrl];
NSURL *url = [NSURL URLWithString:_permalink];
NSArray *activityItems = [[NSArray alloc] initWithObjects:textToShare, imageToShare, url, nil];
Unfortunately, this is not working. What am I doing wrong?
I have also tried to use the AFNetworking library:
UIImage *imageToShare = [UIImage alloc];
[*imageToShare setImageWithURL:[NSURL URLWithString:_iconUrl]];
This is not working too.
_iconUrl is something like http://www.mysite.com/picture.png
Thank you, Francesco
Try with:
UIImage *imageToShare = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:[NSString stringWithFormat:#"%#", _iconUrl]]]];
Matteo
To use remote images, I implemented a UIActivityItemProvider subclass that downloads the image when requested by the UIActivityViewController. UIActivityViewController calls your UIActivityItemProvider on a background thread so at least it doesn't block the main UI. I use a synchronous call just like Matteo suggests inside my UIActivityItemProvider. However, it's really still not a great solution because it just delays when you have to go do the expensive download. UIActivityViewController doesn't request your data until the user picks one of the activity icons in the view controller. At that time, it calls the UIActivityItemProvider to get the data. So you get a delay at this time.
I wondering how you would retrieve the path of the file that a user has dragged and dropped into a cocoa application. For example: User drags a file named test from his/her desktop. Then the cocoa application would say: Users/currentusername/Desktop/test
Thanks for the help!
I just downloaded Apple's "CocoaDragAndDrop" sample code and tried it out.
When I drag in a PNG file from the Finder into the running app, the title of the window changes to the path of the image that was dragged in.
Looking inside the sample code, I can see a file URL is included in the Pasteboard:
//if the drag comes from a file, set the window title to the filename
fileURL=[NSURL URLFromPasteboard: [sender draggingPasteboard]];
[[self window] setTitle: fileURL!=NULL ? [fileURL absoluteString] : #"(no name)"];
Try this technique in your own code and modify it for taste.
The accepted answer is no longer working with Xcode 6.
I've found this methode to get the same result:
NSURL*fileURL = [NSURL URLFromPasteboard: [sender draggingPasteboard]];
NSString *filePath = [fileURL path];
[[self window] setTitle:filePath];
Currently working on developing a similar interface, I’ve understood that the OP had asked for path, not URL retrieval. It seems the suggested OS X 10.10 (XCode6) workaround for the accepted answer has issues in refusing to drag and drop content between windows.
However, avoiding declaring NSString *filePath, but simply substituting the [fileURL absoluteString] method with [fileURL path] method in line 175 of DragDropImageView.m of the suggested sample code instead, seems to solve it:
fileURL=[NSURL URLFromPasteboard: [sender draggingPasteboard]];
[[self window] setTitle: fileURL!=NULL ? [fileURL path] : #"(no name)"];
It compiles and runs as devised in Xcode4 through Xcode6, SDK 10.8-10.10, AFAICT.
Hope this can help.
I have a custom icon file (MyApp.icns) set up for my Cocoa App. How can I access an NSImage representation of the icon from within my application?
Something like the following would be perfect:
NSImage * iconImage = [MyApplication defaultIconAsImage];
But I'm sure it isn't that easy :)
I can, of course, get a path to the icon file as follows:
NSString * iconPath = [[NSBundle mainBundle]
pathForResource:#"MyApp" ofType:#"icns"];
But it seems to me that there should be some kind of standard way to access the icon file for the application, other than calling it by name, since the name could change.
What is the proper way to do this?
[NSApp applicationIconImage]
Just for completeness - This is how you get the icon for any application or file on your system.
NSImage *iconImage = [[NSWorkspace sharedWorkspace] iconForFile:#"path"];
Pass in the path to the application bundle for an application icon or the path to a file for the icon associated with the file.
Note that -[NSApplication applicationIconImage] doesn't return the correct icon when a custom icon is pasted onto the app. Then you need to do:
NSString* appPath = [[NSBundle mainBundle] bundlePath];
NSImage* appIcon = [[NSWorkspace sharedWorkspace] iconForFile:appPath];
(Reference the dock icon code I wrote for Chromium: http://src.chromium.org/svn/trunk/src/chrome/browser/ui/cocoa/dock_icon.mm)
This is probably a n00b question so I apologize in advance. I'm working with NSImage for the first time and basically I need to simply take a picture that is in my Resources folder, and have it display in an NSView/NSImageWell when a button is clicked.
NSImage *image = [[NSImage alloc] initWithContentsOfFile:#"tiles.PNG"];
if ( [image isValid] ) {
NSImageView *view = [[NSImageView alloc] init];
[selection setImage:image];
[selection setImageScaling:NSScaleProportionally];
}
else {
--- This line of code always activates meaning my image isn't valid
}
My only guess is that I am getting the path wrong to the image file and I have looked all over for the right way to access it. Another guess is that I have my code wrong. Anybody familiar with this? Thanks!
I work a lot more with the iPhone, but initWithContentsOfFile seems to require a full/relative path, which I assume tiles.PNG wouldn't fulfill.
I'd use the class method imageNamed:(NSString *)name, which will search your bundle for you.
You should use NSBundleManger to locate the image like so:
NSBundle *mb=[NSBundle mainBundle];
NSString *fp=[mb pathForResource:#"titles" ofType:#"PNG"];
UIImage *img=[UIImage imageWithContentsOfFile:fp];
That way you don't have to mess with internal paths yourself. Otherwise, you have to have the path relative to the final built product which is hard to create and maintain.