I have an application that crashes when the user tries to add an attachment from the camera (however it doesn't crash when they opt to load the attachment from their Photo Library).
Here's the method that gets called when an option (either Camera or Photo Library) is selected:
public void AddMedia(UIImagePickerControllerSourceType type)
{
_imagePicker = new UIImagePickerController();
// set our source to the photo library
_imagePicker.SourceType = type;
// set what media types
_imagePicker.MediaTypes = UIImagePickerController.AvailableMediaTypes(type);
_imagePicker.FinishedPickingMedia += Handle_FinishedPickingMedia;
_imagePicker.Canceled += (sender, evt) =>
{
Console.WriteLine("picker cancelled");
_imagePicker.DismissViewController(false, () =>
{
});
};
//PresentModalViewController is depreciated in iOS6 so we use PresentViewController
Parent.PresentViewController(_imagePicker, true, null);
}
And this is the line where it crashes:
_imagePicker.SourceType = type;
Why would it crash when setting it to Camera but not Photo Library? Does it have something to do with how the enum is ordered (Photo Library = 0, Camera = 1, Saved Photos Album = 2)?
Are you trying to reproduce it on simulator? Because you don't have an access to camera on it.
Related
I am using the below code to get pick image in flutter application, when the code is run on Android it works fine, but when the same code is run on iOS it crashes the app while tapping the button to pick the image.
How should I resolve it?
ERROR I GET
Lost connection to device.
final ImagePicker _picker = ImagePicker();
Future getImage() async {
print("get image");
PickedFile image = await _picker.getImage(source: ImageSource.gallery);
if (image != null) {
setState(() {
final File file = File(image.path);
avatarImageFile = file;
isLoading = true;
});
}
}
info.plist properties
You have to add the below permission in Info.plist file in XCode.
Camera :
Key : Privacy - Camera Usage Description
Value : $(PRODUCT_NAME) camera use
Photo :
Key : Privacy - Photo Library Usage Description
Value : $(PRODUCT_NAME) photo use
I followed this blog for taking photos from the gallery and camera. But the selected picture is showing in right rotated form when it comes to the UI in IOS. Problem only occurs when using the camera and I have no issues with the gallery. This feature is working fine in android and UWP.
Screenshot added below:
Code of Camera:
async void CameraClick()
{
try
{
await CrossMedia.Current.Initialize();
if (!CrossMedia.Current.IsCameraAvailable || !CrossMedia.Current.IsTakePhotoSupported)
{
await DisplayAlert("Camera", "No camera available.", "OK");
return;
}
_mediaFile = await CrossMedia.Current.TakePhotoAsync(new Plugin.Media.Abstractions.StoreCameraMediaOptions
{
Directory = "Sample",
Name = "test.jpg",
AllowCropping = true
});
if (_mediaFile == null)
return;
profileImage.Source = ImageSource.FromStream(() =>
{
isPicture = true;
return _mediaFile.GetStream();
});
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine("Exception:>" + ex);
}
}
Device : IOS SE
Version of the media plugin: 3.1.1
Control I am using to display the image : Xam.plugins.Forms.Imagecircle 2.0.2(For android and UWP I am using 1.8.1)
Gallery pictures are working fine and the issue is only when taking pictures using the camera. No issues in android and UWP part.
Cause:
This is a common experience even outside of Xamarin. It is caused by iOS.
A UIImage has a property imageOrientation, which instructs the
UIImageView and other UIImage consumers to rotate the raw image data.
There's a good chance that this flag is being saved to the exif data
in the uploaded jpeg image, but the program you use to view it is not
honoring that flag.
Solution:
In the Issues part in jamesmontemagno/MediaPlugin in Github, there are several issues like the problem you meet. Seems using GetStreamWithImageRotatedForExternalStorage will fix this issue.
You can refer to:
https://github.com/jamesmontemagno/MediaPlugin/issues/333
In another way, you can rotate the image yourself.
Here are some links that might help you:
iOS Image Orientation has Strange Behavior
iOS UIImagePickerController result image orientation after upload
iOS: Image get rotated 90 degree after saved as PNG representation data
I faced this problem in the last few months on iOS.
The solution for this is add one more line in your code that is:- SaveMetaData = false,
async void CameraClick()
{
try
{
await CrossMedia.Current.Initialize();
if (!CrossMedia.Current.IsCameraAvailable || !CrossMedia.Current.IsTakePhotoSupported)
{
await DisplayAlert("Camera", "No camera available.", "OK");
return;
}
_mediaFile = await CrossMedia.Current.TakePhotoAsync(new Plugin.Media.Abstractions.StoreCameraMediaOptions
{
Directory = "Sample",
Name = "test.jpg",
AllowCropping = true,
SaveMetaData = false
});
if (_mediaFile == null)
return;
profileImage.Source = ImageSource.FromStream(() =>
{
isPicture = true;
return _mediaFile.GetStream();
});
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine("Exception:>" + ex);
}
}
I am trying to launch the default iOS camera application for video recording but it does not work.
Whenever I launch the application it crashes and and does not show any error log or any other error messages.
The following code works perfectly if I set the imagePicker.CameraCaptureMode to UIImagePickerControllerCameraCaptureMode.Photo.
var imagePicker = new UIImagePickerController();
imagePicker.SourceType = UIImagePickerControllerSourceType.Camera;
imagePicker.CameraCaptureMode = UIImagePickerControllerCameraCaptureMode.Video;
var imagePickerDelegate = new ImagePickerDelegate(this);
imagePicker.Delegate = imagePickerDelegate;
NavigationController.PresentModalViewController(imagePicker, true);
Thanks in advance
I got it to work by doing this:
var imagePicker = new UIImagePickerController();
imagePicker.SourceType = UIImagePickerControllerSourceType.Camera;
imagePicker.MediaTypes = new string[]{ UTType.Movie }; // ADD this
var imagePickerDelegate = new ImagePickerDelegate(this);
imagePicker.Delegate = imagePickerDelegate;
NavigationController.PresentModalViewController(imagePicker, true);
Also you can set your delegate calls like so:
imagePicker.FinishedPickingMedia += Handle_FinishedPickingMedia;
imagePicker.Canceled += Handle_Canceled;
Then create these methods:
protected void Handle_FinishedPickingMedia(object sender, UIImagePickerMediaPickedEventArgs e)
{
//code to handle picking media
}
void Handle_Canceled(object sender, EventArgs e)
{
imagePicker.DismissViewController(true, null);
}
Update
In iOS 10 you need to add the permissions and provide the description as to why you are asking for the permission in the info.plist
see here:
iOS 10 - Changes in asking permissions of Camera, microphone and Photo Library causing application to crash
I've downloaded an image from a web service and either need to open the image within safari or save it to the camera roll. I've tried to open the image in Safari, but safari doesn't open.
var safari = UIApplication.SharedApplication;
var uri = new NSUrl(path); // where the image is saved to
safari.OpenUrl(uri);
This compiles, but Safari doesn't launch
The code I have for saving to the camera roll is this
PHPhotoLibrary.SharedPhotoLibrary.PerformChanges(() =>
{
using (var pool3 = new NSAutoreleasePool())
{ pool3.InvokeOnMainThread(delegate ()
{ PHAssetChangeRequest.FromImage((UIImage)UIImage.FromFile(path));
});
}
}, (t, err) =>
{
if (!t)
{
using (var pool1 = new NSAutoreleasePool())
{
pool1.InvokeOnMainThread(delegate () {
var alert = new UIAlertView("Error saving file", "An error has occured saving your photo to the camera roll", null, "OK");
alert.Show();
};
}
else
{
using (var pool2 = new NSAutoreleasePool())
{
pool2.InvokeOnMainThread(delegate (){
var alert = new UIAlertView("Image saved", "Your image has been saved to the camera roll", null, "OK");
alert.Show();
});
};
}
});
which crashes and burns (the correct permissions have been set)
I'm at a loss as to why neither of these work as they seem to be correct.
You can't open a local path with safari, that isn't allowed on iOS (apps are sandboxed) so you'll need to use either a remote URL or have your own UIWebView or WKWebView or a SFSafariViewController to display that image in a safari like experience. Here is a blog post in the Xamarin Site about the subject.
Also here is a nice recipe in the Xamarin site to save a photo into the camera roll, you can do your own solution based on it.
Hope this helps!
I'm currently trying to get an mp4 to play in my app. The MP4 plays fine in vlc (basic check to ensure it's not broken).
The code I'm using looks like this
private void startAnimation()
{
using (var pool = new NSAutoreleasePool())
{
InvokeOnMainThread(() =>
{
player = new MPMoviePlayerController(NSUrl.FromFilename("Graphics/videos/noaudio-data-download.mp4"))
{
AllowsAirPlay = true,
Fullscreen = true,
ScalingMode = MPMovieScalingMode.Fill,
RepeatMode = MPMovieRepeatMode.One,
SourceType = MPMovieSourceType.File,
ShouldAutoplay = true,
ControlStyle = MPMovieControlStyle.Fullscreen,
};
player.View.Frame = View.Bounds;
View.AddSubview(player.View);
View.BringSubviewToFront(player.View);
player.PrepareToPlay();
player.Play();
});
}
}
private void stopAnimation()
{
player.Stop();
player.Dispose();
}
All I get is a black screen and the incredibly unhelpful error
2014-01-05 22:00:44.995 ftrack2ios[85614:80b] _itemFailedToPlayToEnd: {
kind = 1;
new = 2;
old = 0;
}
From what I've read on here and other forums, this error can be down to a pile of different reasons, most of them seem to be down to resizing.
The error occurs on a device as well as on the simulator. I'm not sure if this in iOS 7 issue as it used to work on iOS 6 after some playing around.
I have same problem, and I found that
https://discussions.apple.com/message/23203292#23203292
http://www.techisky.com/how-to/play-mp4-on-ios-7-ipad.html