public override void OnTakePicturesComplete(MultiAssetEventArgs pictures) { if (pictures == null) { return; } ImagesAttach.Clear(); ImagesToAddCount = pictures.Assets.Length; CallbacksCount = 0; var imageManager = new PHImageManager(); foreach (var asset in pictures.Assets) { imageManager.RequestImageData(asset, new PHImageRequestOptions { DeliveryMode = PHImageRequestOptionsDeliveryMode.HighQualityFormat }, (NSData data, NSString dataUti, UIImageOrientation orientation, NSDictionary info) => { double?latFinal = null; double?lonFinal = null; if (data != null) { GetLocationFromImageData(data, ref latFinal, ref lonFinal); } var picture = new CameraPicture { Image = UIImage.LoadFromData(data), Data = data, Lat = latFinal, Lon = lonFinal }; AddImagesIfRequired(picture); }); } }
async void Picker_FinishedPickingAssets(object sender, MultiAssetEventArgs args) { PHImageManager imageManager = new PHImageManager(); Console.WriteLine("User finished picking assets. {0} items selected.", args.Assets.Length); _preselectedAssets = args.Assets; // For demo purposes: just show all chosen pictures in order every second foreach (var asset in args.Assets) { imagePreview.Image = null; // Get information about the asset, e.g. file patch asset.RequestContentEditingInput(new PHContentEditingInputRequestOptions(), (input, _) => { Console.WriteLine(input.FullSizeImageUrl); }); imageManager.RequestImageForAsset(asset, new CGSize(asset.PixelWidth, asset.PixelHeight), PHImageContentMode.Default, null, (image, info) => { imagePreview.Image = image; }); await Task.Delay(1000); } }
private void Handle_PickedImages(object sender, MultiAssetEventArgs e) { ImageEntries = new List <ImageEntry>(); var phAssetArray = e.Assets; var manager = PHImageManager.DefaultManager; using (var options = new PHImageRequestOptions { Synchronous = true, DeliveryMode = PHImageRequestOptionsDeliveryMode.HighQualityFormat, ResizeMode = PHImageRequestOptionsResizeMode.Exact }) { foreach (var asset in phAssetArray) { if (asset.MediaType == PHAssetMediaType.Video) { HandleVideoSelected(); return; } manager.RequestImageForAsset(asset, new CGSize(asset.PixelWidth, asset.PixelHeight), PHImageContentMode.Default, options, (result, info) => { var resources = PHAssetResource.GetAssetResources(asset); var name = resources != null && resources.Length > 0 ? resources[0].OriginalFilename : string.Empty; ImageEntries.Add(new ImageEntry(result.ToSKImage(), name, (DateTime)asset.CreationDate, null)); }); } LaunchAnalysisScreen(ImageEntries); } }
void PickerController_FinishedPickingAssets(object sender, MultiAssetEventArgs args) { List <object> encodedImages = new List <object>(); foreach (var asset in args.Assets) { PHImageManager imageManager = new PHImageManager(); // we make sure the image are saved in the same quality with this option PHImageRequestOptions options = new PHImageRequestOptions { NetworkAccessAllowed = true, DeliveryMode = PHImageRequestOptionsDeliveryMode.HighQualityFormat, Synchronous = true }; imageManager.RequestImageForAsset(asset, new CGSize(asset.PixelWidth, asset.PixelHeight), PHImageContentMode.Default, options, (image, info) => { byte[] dataBytes = new byte[image.AsPNG().Length]; System.Runtime.InteropServices.Marshal.Copy(image.AsPNG().Bytes, dataBytes, 0, Convert.ToInt32(image.AsPNG().Length)); encodedImages.Add(dataBytes); }); } // post the message with the list attached MessagingCenter.Send <object, object>(this, MessagingKeys.DidFinishSelectingImages, encodedImages); }
private async void FinishedPickingAssets(object sender, MultiAssetEventArgs args) { var results = new List <MediaFile>(24); var tcs = new TaskCompletionSource <IList <MediaFile> >(); Debug.WriteLine("User finished picking assets. {0} items selected.", args.Assets.Length); _preselectedAssets = args.Assets; // For demo purposes: just show all chosen pictures in order every second foreach (var asset in _preselectedAssets) { // Get information about the asset, e.g. file patch asset.RequestContentEditingInput(new PHContentEditingInputRequestOptions(), completionHandler: (input, options) => { var path = input.FullSizeImageUrl.ToString(); path = path.Replace("file://", ""); var fileName = System.IO.Path.GetFileNameWithoutExtension(path); var ext = System.IO.Path.GetExtension(path); var imgOption = MediaFileGetImageOptions.CreateDefaultThumb(); var thumbImageBytes = AssetImageService.GetImageBytes(asset, imgOption); var thumbnailImagePath = MediaFileHelper.GetOutputPath(MediaFileType.Image, "TmpMedia", $"{fileName}-THUMBNAIL{ext}"); System.IO.File.WriteAllBytes(thumbnailImagePath, thumbImageBytes); results.Add(new MediaFile { Path = path, PreviewPath = thumbnailImagePath, Type = MediaFileType.Image }); }); await Task.Delay(250); } tcs.TrySetResult(results); _mediaPickTcs?.TrySetResult(await tcs.Task); _preselectedAssets = null; }
async void FinishedPickingAssets(object sender, MultiAssetEventArgs args) { IList <MediaFile> results = new List <MediaFile>(); TaskCompletionSource <IList <MediaFile> > tcs = new TaskCompletionSource <IList <MediaFile> >(); var options = new Photos.PHImageRequestOptions() { NetworkAccessAllowed = true }; options.Synchronous = false; options.ResizeMode = PHImageRequestOptionsResizeMode.Fast; options.DeliveryMode = PHImageRequestOptionsDeliveryMode.HighQualityFormat; bool completed = false; for (var i = 0; i < args.Assets.Length; i++) { var asset = args.Assets[i]; string fileName = string.Empty; if (UIDevice.CurrentDevice.CheckSystemVersion(9, 0)) { fileName = PHAssetResource.GetAssetResources(asset).FirstOrDefault().OriginalFilename; } switch (asset.MediaType) { case PHAssetMediaType.Video: PHImageManager.DefaultManager.RequestImageForAsset(asset, new SizeF(150.0f, 150.0f), PHImageContentMode.AspectFill, options, async(img, info) => { var startIndex = fileName.IndexOf(".", StringComparison.CurrentCulture); string path = ""; if (startIndex != -1) { path = FileHelper.GetOutputPath(MediaFileType.Image, TemporalDirectoryName, $"{fileName.Substring(0, startIndex)}-THUMBNAIL.JPG"); } else { path = FileHelper.GetOutputPath(MediaFileType.Image, TemporalDirectoryName, string.Empty); } if (!File.Exists(path)) { img.AsJPEG().Save(path, true); } TaskCompletionSource <string> tvcs = new TaskCompletionSource <string>(); var vOptions = new PHVideoRequestOptions(); vOptions.NetworkAccessAllowed = true; vOptions.Version = PHVideoRequestOptionsVersion.Original; vOptions.DeliveryMode = PHVideoRequestOptionsDeliveryMode.FastFormat; PHImageManager.DefaultManager.RequestAvAsset(asset, vOptions, (avAsset, audioMix, vInfo) => { var vPath = FileHelper.GetOutputPath(MediaFileType.Video, TemporalDirectoryName, fileName); if (!File.Exists(vPath)) { AVAssetExportSession exportSession = new AVAssetExportSession(avAsset, AVAssetExportSession.PresetHighestQuality); exportSession.OutputUrl = NSUrl.FromFilename(vPath); exportSession.OutputFileType = AVFileType.QuickTimeMovie; exportSession.ExportAsynchronously(() => { Console.WriteLine(exportSession.Status); tvcs.TrySetResult(vPath); //exportSession.Dispose(); }); } }); var videoUrl = await tvcs.Task; var meFile = new MediaFile() { Type = MediaFileType.Video, Path = videoUrl, PreviewPath = path }; results.Add(meFile); OnMediaPicked?.Invoke(this, meFile); if (args.Assets.Length == results.Count && !completed) { completed = true; tcs.TrySetResult(results); } }); break; default: Photos.PHImageManager.DefaultManager.RequestImageData(asset, options, (data, dataUti, orientation, info) => { string path = FileHelper.GetOutputPath(MediaFileType.Image, TemporalDirectoryName, fileName); if (!File.Exists(path)) { Debug.WriteLine(dataUti); var imageData = data; //var image = UIImage.LoadFromData(imageData); //if (imageScale < 1.0f) //{ // //begin resizing image // image = image.ResizeImageWithAspectRatio((float)imageScale); //} //if (imageQuality < 100) //{ // imageData = image.AsJPEG(Math.Min(imageQuality,100)); //} imageData?.Save(path, true); } var meFile = new MediaFile() { Type = MediaFileType.Image, Path = path, PreviewPath = path }; results.Add(meFile); OnMediaPicked?.Invoke(this, meFile); if (args.Assets.Length == results.Count && !completed) { completed = true; tcs.TrySetResult(results); } }); break; } } mediaPickTcs?.TrySetResult(await tcs.Task); }
async void FinishedPickingAssets(object sender, MultiAssetEventArgs args) { var picker = (GMImagePickerController)sender; picker.FinishedPickingAssets -= FinishedPickingAssets; picker.Canceled -= OnPickerCanceled; // synchronous: NO.异步。 // a.deliveryMode: Opportunistic: 会返回多张图片 // 1). ResizeMode.None: 先返回低清的缩略图,再返回原图大小 // 2). ResizeMode.Fast: 先返回低清的缩略图,再返回的原图会使用targetSize来最优解码图片,获得的图片大小可能比targetSize大 // 3). ResizeMode.Exact: 先返回低清的缩略图,再返回的原图会使用targetSize的高质量图 // b.deliveryMode: HighQualityFormat: 只会返回一张高清图片 // 1). ResizeMode.None: 返回的是原图大小 // 2). ResizeMode.Fast: 当原图是压缩图时,会使用targetSize来最优解码图片,获得的图片大小可能比targetSize大 // 3). ResizeMode.Exact: 解压和Fast一样,但是返回的是指定targetSize的高质量图 // c.deliveryMode: FastFormat: 只会返回一张图片,并且可能是低清图 // 1). ResizeMode.None: 返回一张低清图 // 2). ResizeMode.Fast: 返回一张低清图 // 3). ResizeMode.Exact: 返回一张低清图 var options = new PHImageRequestOptions { NetworkAccessAllowed = true, Synchronous = false, DeliveryMode = PHImageRequestOptionsDeliveryMode.HighQualityFormat, ResizeMode = PHImageRequestOptionsResizeMode.Exact, }; var tcs = new TaskCompletionSource <object>(); bool completed = false; List <FileData> result = new List <FileData>(); for (var i = 0; i < args.Assets.Length; i++) { var asset = args.Assets[i]; switch (asset.MediaType) { case PHAssetMediaType.Video: // 未测 PHImageManager.DefaultManager.RequestImageForAsset( asset, new SizeF(FileData.ThumbSize, FileData.ThumbSize), PHImageContentMode.AspectFit, options, async(img, info) => { // 获取路径,麻烦 TaskCompletionSource <NSUrl> tcsUrl = new TaskCompletionSource <NSUrl>(); var vOptions = new PHVideoRequestOptions { NetworkAccessAllowed = true, Version = PHVideoRequestOptionsVersion.Original, DeliveryMode = PHVideoRequestOptionsDeliveryMode.FastFormat, }; PHImageManager.DefaultManager.RequestAvAsset( asset, vOptions, (avAsset, audioMix, vInfo) => { if (avAsset is AVUrlAsset avUrl) { tcsUrl.TrySetResult(avUrl.Url); } else { tcsUrl.TrySetResult(null); } }); NSUrl url = await tcsUrl.Task; // 生成文件描述和缩略图 var fd = ParseUrl(url); fd.Desc = $"{asset.PixelWidth} x {asset.PixelHeight} ({fd.Ext.TrimStart('.')})"; fd.ThumbPath = Path.Combine(Kit.CachePath, Kit.NewGuid + "-t.jpg"); img.AsJPEG().Save(fd.ThumbPath, true); result.Add(fd); if (args.Assets.Length == result.Count && !completed) { completed = true; tcs.TrySetResult(null); } }); break; default: PHImageManager.DefaultManager.RequestImageForAsset( asset, new SizeF(FileData.ThumbSize, FileData.ThumbSize), PHImageContentMode.AspectFit, options, async(img, info) => { // 获取路径,麻烦 TaskCompletionSource <NSUrl> tcsUrl = new TaskCompletionSource <NSUrl>(); asset.RequestContentEditingInput(new PHContentEditingInputRequestOptions(), (input, _) => { tcsUrl.TrySetResult(input.FullSizeImageUrl); }); NSUrl url = await tcsUrl.Task; // 生成文件描述和缩略图 var fd = ParseUrl(url); fd.Desc = $"{asset.PixelWidth} x {asset.PixelHeight} ({fd.Ext.TrimStart('.')})"; if (asset.PixelWidth > FileData.ThumbSize || asset.PixelHeight > FileData.ThumbSize) { fd.ThumbPath = Path.Combine(Kit.CachePath, Kit.NewGuid + "-t.jpg"); img.AsJPEG().Save(fd.ThumbPath, true); } result.Add(fd); if (args.Assets.Length == result.Count && !completed) { completed = true; tcs.TrySetResult(null); } }); break; } } // 用临时tcs等待比直接在回调方法中用_tcs速度大幅提高!!! await tcs.Task; _tcs.TrySetResult(result); }
private void Picker_FinishedPickingAssets(object sender, MultiAssetEventArgs args) { _multiAsset = args; // imgView.Image= pHAsset.Location // uIImage. = pHAsset.Location(); // List< UIImage> uIImage = new List<UIImage> (); //foreach (PHAsset asset in args.Assets) //{// // string date = string.Empty; // UIImageView imag = new UIImageView(); // iEbutton button = new iEbutton(); // string endex = string.Empty; // UIImage image1 = new UIImage(); // AVPlayer avplayer = new AVPlayer(); // if (icount >= 0) { // if (asset.MediaType == PHAssetMediaType.Video) // { // isvideo = true; // // NSUrl videoUrl = null; // imageManager.RequestAvAsset(asset, null, (avsset, avaudio, NsD) => // { // // string UrlConvertString = null; // var videoUrl = ((AVFoundation.AVUrlAsset)avsset).Url; // string UrlConvertString = (videoUrl.AbsoluteString); // UrlVideoString.Add(UrlConvertString); // }); // } // imageManager.RequestImageForAsset(asset, // new CGSize(asset.PixelWidth, asset.PixelHeight), // PHImageContentMode.Default, // null, // (image, info) => // { // // endex = image.AccessibilityPath.ToString(); // // endex = "jpg"; // // // if (!isvideo) // { // im = image; // // Imagedata.Add(image); // byte[] myByteArray; // using (NSData imageData = image.AsPNG()) // { // myByteArray = new Byte[imageData.Length]; // System.Runtime.InteropServices.Marshal.Copy(imageData.Bytes, myByteArray, 0, Convert.ToInt32(imageData.Length)); // } // //node // string documentsPath = Environment.GetFolderPath(Environment.SpecialFolder.Personal); // string localFilename = "text.jpg"; //same if I save the file as .mp4 // var localPath = Path.Combine(documentsPath, localFilename); // File.WriteAllBytes(localPath, myByteArray); // byte[] n = File.ReadAllBytes(localPath); // string random = GeneratePassword(7); // DataSql.process(library.File(2), library.INSERT(2), DataSql_Parameters(random + ".jpg", n, "jpg/png")); // //node // } // // button.creation(i, image); // // date = DateTime.Now.ToString("MMddHHmmss"); // icount--; // Thread.Sleep(500); // }); //} // //button.getText(date); // //button.click += Button_TouchUpInside; ; // // isvideo = false; // //UIbutton.Add(button); // // lab.Text = ImageView.Count.ToString(); // // Imagedata.Add(image1); //}//end foreach // finshpluse = 1; // imgView.Image = uIImage[uIImage.Count-1]; // scoll = new UIScrollView(); //if (scoll.Frame.Height > scoll.Frame.Width ) //{ // scoll.ContentSize = new CGSize(uiv.Frame.Width -70, ImageView.Count * 15); // putimage(50,70,14); //} //else { scoll.ContentSize = new CGSize(ImageView.Count+20, ImageView.Count * 15); // putimage(80, 90, 40); //} exration(); // im = false; //finshpluse = 1; //todatabese(); ; }
void exration() { //change saveFormGrally.Clear(); // Imagedata.Clear(); int icount = _multiAsset.Assets.Count(); string date = string.Empty; UIImageView imag = new UIImageView(); iEbutton button = new iEbutton(); string endex = string.Empty; PHImageManager imageManager = new PHImageManager(); // bool isvideo = false; UIImage image1 = new UIImage(); // byetdata = new List<byte[]>(); foreach (PHAsset asset in _multiAsset.Assets) { save_Plus save = new save_Plus(); bool isvideo = false; AVPlayer avplayer = new AVPlayer(); if (asset.MediaType == PHAssetMediaType.Video) { isvideo = true; // NSUrl videoUrl = null; imageManager.RequestAvAsset(asset, null, (avsset, avaudio, NsD) => { // string UrlConvertString = null; var videoUrl = ((AVFoundation.AVUrlAsset)avsset).Url; string UrlConvertString = (videoUrl.AbsoluteString); save.Url = UrlConvertString; save.Extension = "Video"; // UrlVideoString.Add(UrlConvertString); }); } imageManager.RequestImageForAsset(asset, new CGSize(asset.PixelWidth, asset.PixelHeight), PHImageContentMode.Default, null, (image, info) => { // endex = image.AccessibilityPath.ToString(); // endex = "jpg"; // if (!isvideo) { save.Image = image; save.Thumbnail = image; save.Extension = "Image"; // byte[] myByteArray; // using (NSData imageData = image.AsPNG()) // { // myByteArray = new Byte[imageData.Length]; // System.Runtime.InteropServices.Marshal.Copy(imageData.Bytes, myByteArray, 0, Convert.ToInt32(imageData.Length)); // } // string random = GeneratePassword(7); //// Imagedata.Add(image); // DataSql.process(library.File(2), library.INSERT(2), DataSql_Parameters(random + ".jpg", myByteArray, "image")); //node } // if isvideo else { save.Thumbnail = image; } isvideo = true; // button.creation(i, image); // date = DateTime.Now.ToString("MMddHHmmss"); icount--; saveFormGrally.Add(save); Thread.Sleep(500); }); } //button.getText(date); //button.click += Button_TouchUpInside; ; // isvideo = false; //UIbutton.Add(button); // lab.Text = ImageView.Count.ToString(); // Imagedata.Add(image1); //nd foreach} _multiAsset = null; todatabese(); finshpluse = 1; }
public virtual void OnTakePicturesComplete(MultiAssetEventArgs pictures) { }
//puls button; private void Picker_FinishedPickingAssets(object sender, MultiAssetEventArgs args) { UrlVideoString.Clear(); Imagedata.Clear(); // Imagedata = null; bool isvideo = false; int i = buttonnumber; PHAsset pHAsset = args.Assets[0]; // imgView.Image= pHAsset.Location // uIImage. = pHAsset.Location(); // List< UIImage> uIImage = new List<UIImage> (); PHImageManager imageManager = new PHImageManager(); foreach (var asset in args.Assets) { // string date = string.Empty; UIImageView imag = new UIImageView(); iEbutton button = new iEbutton(); string endex = string.Empty; UIImage image1 = new UIImage(); AVPlayer avplayer = new AVPlayer(); if (asset.MediaType == PHAssetMediaType.Video) { isvideo = true; // NSUrl videoUrl = null; imageManager.RequestAvAsset(asset, null, (avsset, avaudio, NsD) => { // string UrlConvertString = null; var videoUrl = ((AVFoundation.AVUrlAsset)avsset).Url; string UrlConvertString = (videoUrl.AbsoluteString); UrlVideoString.Add(UrlConvertString); }); } imageManager.RequestImageForAsset(asset, new CGSize(asset.PixelWidth, asset.PixelHeight), PHImageContentMode.Default, null, (image, info) => { // endex = image.AccessibilityPath.ToString(); // endex = "jpg"; // if (isvideo == false) { image1 = image; imag.Image = image; Imagedata.Add(image1); // button.creation(i, image); } // date = DateTime.Now.ToString("MMddHHmmss"); Thread.Sleep(1000); }); //button.getText(date); //button.click += Button_TouchUpInside; ; isvideo = false; //UIbutton.Add(button); // lab.Text = ImageView.Count.ToString(); // Imagedata.Add(image1); } //end foreach // finshpluse = 1; // imgView.Image = uIImage[uIImage.Count-1]; // scoll = new UIScrollView(); //if (scoll.Frame.Height > scoll.Frame.Width ) //{ // scoll.ContentSize = new CGSize(uiv.Frame.Width -70, ImageView.Count * 15); // putimage(50,70,14); //} //else { scoll.ContentSize = new CGSize(ImageView.Count+20, ImageView.Count * 15); // putimage(80, 90, 40); //} finshpluse = 1; todatabese(); }