コメント
コメントの投稿
※名前とタイトルが入力されていないコメントでは他のコメントとの区別ができません。
入力されていないコメントには返信しませんのであらかじめご了承くださいませ。
public interface IImageService
{
void ShowImageGallery();
}
using Xamarin.Forms;
public partial class App : Application
{
private static Image _pickupImage;
private static ContentPage _resultPage;
public static ContentPage ResultPage {
set
{
_resultPage = value;
}
}
public static ImageSource GetPickupImageSource()
{
return _pickupImage.Source;
}
public static void SetPickupImageSource(string filePath)
{
//写真を表示する対象のページが設定されていない場合は処理を中断
if (_resultPage == null)
{
return;
}
_pickupImage = _resultPage.FindByName<Image>("imgPicture");
_pickupImage.Source = ImageSource.FromFile(filePath);
}
public static void SetPickupImageSource(ImageSource source)
{
//写真を表示する対象のページが設定されていない場合は処理を中断
if (_resultPage == null)
{
return;
}
_pickupImage = _resultPage.FindByName<Image>("imgPicture");
_pickupImage.Source = source;
}
public App()
{
MainPage = new NavigationPage(new TestPage());
}
}
[assembly: Dependency(typeof(ImageService))]
public class ImageService : IImageService
{
/// <summary>
/// イメージ選択画面を表示する(外部アプリ)
/// </summary>
public void ShowImageGallery()
{
UIViewController viewController;
UINavigationController navigationController;
viewController = new ContactFriends.iOS.Models.ImagePickerController();
navigationController = this.FindNavigationController();
navigationController.PresentViewController(viewController, true, null);
}
private UINavigationController FindNavigationController()
{
//Check to see if the rootviewcontroller is the navigationcontroller.
foreach (var window in UIApplication.SharedApplication.Windows)
{
if (window.RootViewController == null)
{
continue;
}
if (window.RootViewController.NavigationController != null)
return window.RootViewController.NavigationController;
else
{
UINavigationController val = GetSubController(window.RootViewController.ChildViewControllers);
if (val != null)
return val;
}
}
// var viewcontroller = UIApplication.SharedApplication.KeyWindow.RootViewController as UIViewController;
// viewcontroller.NavigationController = new UINavigationController();
// return new UINavigationController(viewcontroller);
return null;
}
private UINavigationController GetSubController(UIViewController[] controllers)
{
foreach (var controller in controllers)
{
if (controller.NavigationController != null)
return controller.NavigationController;
else
{
UINavigationController val = GetSubController(controller.ChildViewControllers);
if (val != null)
return val;
}
}
return null;
}
}
using CoreGraphics;
using System.IO;
using Xamarin.Forms;
public class ImagePickerController : UIImagePickerController
{
UIImageView _imageView;
public override void ViewDidLoad()
{
base.ViewDidLoad();
View.BackgroundColor = UIColor.White;
var closeButtonItem = new UIBarButtonItem("Close", UIBarButtonItemStyle.Plain, this, null);
this.NavigationItem.LeftBarButtonItem = closeButtonItem;
_imageView = new UIImageView(new CGRect(10, 150, 300, 300));
// set our source to the photo library
this.SourceType = UIImagePickerControllerSourceType.PhotoLibrary;
// set what media types
this.MediaTypes = UIImagePickerController.AvailableMediaTypes(UIImagePickerControllerSourceType.PhotoLibrary);
this.FinishedPickingMedia += Handle_FinishedPickingMedia;
this.Canceled += Handle_Canceled;
}
// Do something when the
void Handle_Canceled(object sender, EventArgs e)
{
Console.WriteLine("picker cancelled");
this.DismissModalViewController(true);
}
// This is a sample method that handles the FinishedPickingMediaEvent
protected void Handle_FinishedPickingMedia(object sender, UIImagePickerMediaPickedEventArgs e)
{
// determine what was selected, video or image
bool isImage = false;
switch (e.Info[UIImagePickerController.MediaType].ToString())
{
case "public.image":
Console.WriteLine("Image selected");
isImage = true;
break;
case "public.video":
Console.WriteLine("Video selected");
break;
}
Console.Write("Reference URL: [" + UIImagePickerController.ReferenceUrl + "]");
// get common info (shared between images and video)
NSUrl referenceURL = e.Info[new NSString("UIImagePickerControllerReferenceUrl")] as NSUrl;
if (referenceURL != null)
Console.WriteLine(referenceURL.ToString());
// if it was an image, get the other image info
if (isImage)
{
// get the original image
UIImage originalImage = e.Info[UIImagePickerController.OriginalImage] as UIImage;
if (originalImage != null)
{
// do something with the image
Console.WriteLine("got the original image");
_imageView.Image = originalImage;
}
// get the edited image
UIImage editedImage = e.Info[UIImagePickerController.EditedImage] as UIImage;
if (editedImage != null)
{
// do something with the image
Console.WriteLine("got the edited image");
_imageView.Image = editedImage;
}
//- get the image metadata
NSDictionary imageMetadata = e.Info[UIImagePickerController.MediaMetadata] as NSDictionary;
if (imageMetadata != null)
{
// do something with the metadata
Console.WriteLine("got image metadata");
}
}
// if it's a video
else
{
// get video url
NSUrl mediaURL = e.Info[UIImagePickerController.MediaURL] as NSUrl;
if (mediaURL != null)
{
//
Console.WriteLine(mediaURL.ToString());
}
}
//App.csのImageSourceにセットする
ImagePickerController.SetAppImageSource(_imageView.Image);
// dismiss the picker
this.DismissModalViewController(true);
}
public static void SetAppImageSource(UIImage image)
{
if (image != null)
{
//取得した画像をバイト配列にコピーして別ストリームで読み直す
byte[] byteArray = null;
using (Stream imageStream = image.AsPNG().AsStream())
{
imageStream.Position = 0;
using (MemoryStream ms = new MemoryStream())
{
imageStream.CopyTo(ms);
byteArray = ms.ToArray();
}
}
var imageSource = ImageSource.FromStream(() => new MemoryStream(byteArray));
App.SetPickupImageSource(imageSource);
}
}
}
<key>NSPhotoLibraryUsageDescription</key>
<string>フォトライブラリを利用します。</string>
public class ImageService : IImageService
{
/// <summary>
/// イメージ選択画面を表示する(外部アプリ)
/// </summary>
public void ShowImageGallery()
{
//アクティビティを取得する
var activity = (Activity)(Forms.Context);
//ギャラリーを表示する
var imageIntent = new Intent();
imageIntent.SetType("image/*");
imageIntent.SetAction(Intent.ActionGetContent);
activity.StartActivityForResult(
Intent.CreateChooser(imageIntent, "Select photo"), 0);
}
}
using Android.Database;
public class MainActivity
{
protected override void OnActivityResult(int requestCode, Result resultCode, global::Android.Content.Intent data)
{
try
{
base.OnActivityResult(requestCode, resultCode, data);
//ImageGalleryからの戻り値
if (resultCode == Result.Ok)
{
string filePath = this.GetSelectedFilePath(data);
App.SetPickupImageSource(filePath);
}
}
catch (Exception ex)
{
System.Diagnostict.Debug.WriteLine(ex.Message + System.Environment.NewLine + ex.StackTrace);
}
}
//イメージギャラリーの戻り値から画像のファイルパスを取得する
private string GetSelectedFilePath(Intent data)
{
string filePath = String.Empty;
ICursor cur = null;
try
{
// 選択した画像のパスを取得する
string[] columns = { Android.Provider.MediaStore.Images.Media.InterfaceConsts.Data };
cur = this.ContentResolver.Query(data.Data, columns, null, null, null);
if (cur != null &&
cur.MoveToFirst())
{
filePath = cur.GetString(0);
}
return filePath;
}
finally
{
if (cur != null)
{
cur.Close();
}
}
}
}
using AppName.Services;
using Xamarin.Forms;
public partial class TestPage : ContentPage
{
public TestPage()
{
//写真タップ時の遷移
TapGestureRecognizer tgr = new TapGestureRecognizer();
tgr.Tapped += (s, e) =>
{
this.OnTapped(s, e);
};
this.imgPicture.GestureRecognizers.Add(tgr);
}
void OnTapped(object sender, EventArgs e)
{
App.ResultPage = this;
DependencyService.Get<IImageService>().ShowImageGallery();
}
}
※名前とタイトルが入力されていないコメントでは他のコメントとの区別ができません。
入力されていないコメントには返信しませんのであらかじめご了承くださいませ。