flash.display.Loader class on iOS - ios

Intro - I am working on a iOS game using Flash CS5.5. In my game i am loading a image via the camera roll, and gonna use the image in the gameplay. I want the image to load back in when the user restarts the game.
Question - Is there a way to access such image again without having to open up the camera roll interface.
Can i use the flash.display.Loader class? Is there any special things i need to do or set? Or is there another way?
I've tried just saving the file, given from the MediaEvent dispatched when you select an image.
But i have no luck using the URL given in the loader object. I am using the example from this page as my base: http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/media/CameraRoll.html.

Yes this is possible. You need to save the image into the apps documents directory using the File class with something like this: (this uses the Adobe JPGEncoder class)
f = File.documentsDirectory.resolvePath("logo.jpg");
stream = new FileStream();
stream.open(f, FileMode.WRITE);
j = new JPGEncoder(80);
var bytes:ByteArray = j.encode(visualLogo.bitmapData);
stream.writeBytes(bytes, 0, bytes.length);
stream.writeBytes(bytes, 0, bytes.bytesAvailable);
stream.close();
//I read the file back in to test that it has been successfully written
stream.openAsync(f, FileMode.READ);
stream.addEventListener(Event.COMPLETE, bringBoardOn, false, 0, true);
Then load it back in using something like this:
f = File.documentsDirectory.resolvePath ("logo.jpg");
if (f.exists == true) {
var _urlRequest:URLRequest = new URLRequest(f.url);
loader=new Loader;
loader.load(_urlRequest);
loader.addEventListener(IOErrorEvent.IO_ERROR, function(e:IOErrorEvent):void{ trace(e) });
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, ImageLoaded, false, 0, true);
_urlRequest = null;
}

Related

Xamarin form: How to load a file on iOS [duplicate]

I want to use UIActivityViewController to share files from my iOS app. The main question for me is how do I handle different file types.
What I'v got so far:
Images
public void OpenInExternalApp(string filepath)
{
if (!File.Exists(filepath))
return;
UIImage uiImage = UIImage.FromFile(filepath);
// Define the content to share
var activityItems = new NSObject[] { uiImage };
UIActivity[] applicationActivities = null;
var activityController = new UIActivityViewController(activityItems, applicationActivities);
if (UIDevice.CurrentDevice.UserInterfaceIdiom == UIUserInterfaceIdiom.Phone)
{
// Phone
UIApplication.SharedApplication.KeyWindow.RootViewController.PresentViewController(activityController, true, null);
}
else
{
// Tablet
var popup = new UIPopoverController(activityController);
UIView view = UIApplication.SharedApplication.KeyWindow.RootViewController.View;
CGRect rect = new CGRect(view.Frame.Width/2, view.Frame.Height, 50, 50);
popup.PresentFromRect(rect, view, UIPopoverArrowDirection.Any, true);
}
}
Don't know if from the memory management aspect it is a good idea to load the image at once. What will happen if the image is too big for holding it completely in RAM? See here for example.
Strings
var activityItems = new NSObject[] { UIActivity.FromObject(new NSString(text)) };
Only text.
NSUrl
NSUrl url = NSUrl.CreateFileUrl(filepath, false, null);
Here in most cases the same app appear. But for example the PDF reader doesn't appear for a PDF file. The preview in mail on the other side shows Adobe Acrobat.
Everything
var activityItems = new NSObject[] { NSData.FromFile(filepath) };
The last approach has the disadvantage that not all apps are displayed, which for example could open a PDF file. Also this applies.
I want to use all types of files. I don't think a subclass of UIActivity would help here. Perhaps a sublcass of UIActivityItemProvider?
Side note: You can also post your solutions in Objective C/Swift.
I tried to implement UIActivityItemProvider, but here again not all apps where shown for the corresponding filetype. E.g. for a docx-document Word was not shown.
Now I switched to UIDocumentInteractionController and now there are many apps available.
UIDocumentInteractionController documentController = new UIDocumentInteractionController();
documentController.Url = new NSUrl(filepath, false);
string fileExtension = Path.GetExtension(filepath).Substring(1);
string uti = UTType.CreatePreferredIdentifier(UTType.TagClassFilenameExtension.ToString(), fileExtension, null);
documentController.Uti = uti;
UIView presentingView = UIApplication.SharedApplication.KeyWindow.RootViewController.View;
documentController.PresentOpenInMenu(CGRect.Empty, presentingView, true);
Imho there are too many apps, because the file type xml should not be really be supported by a PDF reader, but it is. Nevertheless, it seems to work now thanks to this post:
In general if you’re sharing an image or url, you might want to use a UIActivityViewController. If you’re sharing a document, you might want to use a UIDocumentInteractionController.

Use UIActivityViewController to share all types of files

I want to use UIActivityViewController to share files from my iOS app. The main question for me is how do I handle different file types.
What I'v got so far:
Images
public void OpenInExternalApp(string filepath)
{
if (!File.Exists(filepath))
return;
UIImage uiImage = UIImage.FromFile(filepath);
// Define the content to share
var activityItems = new NSObject[] { uiImage };
UIActivity[] applicationActivities = null;
var activityController = new UIActivityViewController(activityItems, applicationActivities);
if (UIDevice.CurrentDevice.UserInterfaceIdiom == UIUserInterfaceIdiom.Phone)
{
// Phone
UIApplication.SharedApplication.KeyWindow.RootViewController.PresentViewController(activityController, true, null);
}
else
{
// Tablet
var popup = new UIPopoverController(activityController);
UIView view = UIApplication.SharedApplication.KeyWindow.RootViewController.View;
CGRect rect = new CGRect(view.Frame.Width/2, view.Frame.Height, 50, 50);
popup.PresentFromRect(rect, view, UIPopoverArrowDirection.Any, true);
}
}
Don't know if from the memory management aspect it is a good idea to load the image at once. What will happen if the image is too big for holding it completely in RAM? See here for example.
Strings
var activityItems = new NSObject[] { UIActivity.FromObject(new NSString(text)) };
Only text.
NSUrl
NSUrl url = NSUrl.CreateFileUrl(filepath, false, null);
Here in most cases the same app appear. But for example the PDF reader doesn't appear for a PDF file. The preview in mail on the other side shows Adobe Acrobat.
Everything
var activityItems = new NSObject[] { NSData.FromFile(filepath) };
The last approach has the disadvantage that not all apps are displayed, which for example could open a PDF file. Also this applies.
I want to use all types of files. I don't think a subclass of UIActivity would help here. Perhaps a sublcass of UIActivityItemProvider?
Side note: You can also post your solutions in Objective C/Swift.
I tried to implement UIActivityItemProvider, but here again not all apps where shown for the corresponding filetype. E.g. for a docx-document Word was not shown.
Now I switched to UIDocumentInteractionController and now there are many apps available.
UIDocumentInteractionController documentController = new UIDocumentInteractionController();
documentController.Url = new NSUrl(filepath, false);
string fileExtension = Path.GetExtension(filepath).Substring(1);
string uti = UTType.CreatePreferredIdentifier(UTType.TagClassFilenameExtension.ToString(), fileExtension, null);
documentController.Uti = uti;
UIView presentingView = UIApplication.SharedApplication.KeyWindow.RootViewController.View;
documentController.PresentOpenInMenu(CGRect.Empty, presentingView, true);
Imho there are too many apps, because the file type xml should not be really be supported by a PDF reader, but it is. Nevertheless, it seems to work now thanks to this post:
In general if you’re sharing an image or url, you might want to use a UIActivityViewController. If you’re sharing a document, you might want to use a UIDocumentInteractionController.

Titanium / How to temporarily store a photo to use in a later function

Titanium / for an iOS App:
How can I manage to take a photo, and then use this one later in a new function to for example show the photo, and put a slightly larger duplicate of it with a transparency on top of itself?
Note that I tried to edit the answer from Mitul Bhalia with the following, but the edit got knocked back. So here's how you do it:
After taking the image, you can store it as a variable in the global object, Alloy.Globals. You can then access this else where or later on in your app.
For example:
takePhotoButton.addEventListener('click', function(){
Titanium.Media.showCamera({
success:function(event) {
if(event.mediaType === Ti.Media.MEDIA_TYPE_PHOTO) {
// Store the file in a variable
var image = event.media;
// Store the image in the global object
Alloy.Globals.temporaryImage = image;
} else {
alert("got the wrong type back ="+event.mediaType);
}
},
...
And somewhere else in your app, after the image has been stored, for example:
var anImage = Ti.UI.createImageView({ image: Alloy.Globals.temporaryImage })
Also note that extensive use of the global object can cause memory issues, so try not to overdo it.
If you want to save photo then you can use Alloy.Globals to save data globally so you can use it later.
Alloy.Globals.photo = blob object;
Here is how I am currently solving my problem:
takePhotoButton.addEventListener('click', function(){
Titanium.Media.showCamera({
success:function(event) {
if(event.mediaType === Ti.Media.MEDIA_TYPE_PHOTO) {
// Store the file in a variable
var image = event.media;
var filename = 'myPhoto.jpg';
takenPhoto = Titanium.Filesystem.getFile(Titanium.Filesystem.applicationDataDirectory, filename);
takenPhoto.write(image);
} else {
alert("got the wrong type back ="+event.mediaType);
}
},
...
and then later I can use takenPhoto to show the picture I have taken with the camera.
But I do not really know if this is the best way and if it is even correct, as I do not use 'var' to initiate takenPhoto. But if I use 'var' I cannot use takenPhoto outside of the function.

Using Isolated Storage Images in the HubTiles

I let the users take images in my app and save the images in the Isolated Storage. I also use HubTiles in my app but the HubTiles use a Uri in their Source property but it can't recognize the isostore:/..... Uris..
Any idea how can I fix this issue?
You are not the only one experiencing that the isostore:/ URIs are not functioning in every place an URI is expected. So it seems like you need to take the more traditional approach and load the image by hand:
// define data array to hold image data to be read from isolated storage
byte[] imageBytes;
using (IsolatedStorageFile myIsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication())
{
// open image file
using (IsolatedStorageFileStream fileStream = myIsolatedStorage.OpenFile("MyPreviouslySavedImage.jpg", FileMode.Open, FileAccess.Read))
{
// allocate array large enough to hold the whole file
imageBytes = new byte[fileStream.Length];
// read all data to memory
fileStream.Read(imageBytes, 0, imageBytes.Length);
fileStream.Close();
}
}
// create memory stream and bitmap
MemoryStream memoryStream = new MemoryStream(imageBytes);
BitmapImage bitmapImage = new BitmapImage();
// memory stream is source of bitmap
bitmapImage.SetSource(memoryStream);
// finally assign image to hub tile
hubTile1.Source = bitmapImage;
This works well (if there is an image in the isostore of course).

access netStream or movieClip from a loop AS2

I've got a load of videos
var ns1:NetStream = new NetStream(nc);
container1.compMa.theVideo.attachVideo(ns1);
ns1.play("sukh_diesel.flv", 1);
//
var ns2:NetStream = new NetStream(nc);
container2.compMa.theVideo.attachVideo(ns2);
ns2.play("sukh_beneath.flv", 1);
//and 4 more, which I've left out to be concise
I want to pause them with
function pauseVid(){
this.ns1.pause();
for(i=1;i<7;i++){
this["ns"+i]pause();
}
}
the commented out line:
this.ns1.pause()
works, but when I try it in a loop it can't access it?
Have you tried using eval to access your stream by name?
function pauseVid(){
var localStream:NetStream;
for(i=1;i<=7;i++){
localStream = eval("ns"+i);
localStream.pause();
}
}
I would recommend keeping track of your objects with an array of streams for example. This way you would avoid change the upper limit of your for statement.

Resources