コード例 #1
0
        private static PHAssetCollection FindOrCreateAlbum(string albumName)
        {
            var albums      = PHAssetCollection.FetchAssetCollections(PHAssetCollectionType.Album, PHAssetCollectionSubtype.AlbumRegular, null);
            var customAlbum = (PHAssetCollection)albums.FirstOrDefault(s => ((PHAssetCollection)s).LocalizedTitle.Equals(albumName));

            if (customAlbum == null)
            {
                var success = PHPhotoLibrary.SharedPhotoLibrary.PerformChangesAndWait(
                    () =>
                {
                    PHAssetCollectionChangeRequest.CreateAssetCollection(albumName);
                }, out var error
                    );
                if (success)
                {
                    albums      = PHAssetCollection.FetchAssetCollections(PHAssetCollectionType.Album, PHAssetCollectionSubtype.AlbumRegular, null);
                    customAlbum = (PHAssetCollection)albums.FirstOrDefault(s => ((PHAssetCollection)s).LocalizedTitle.Equals(albumName));
                }
                else
                {
                    Console.WriteLine(error);
                    customAlbum = null;
                }
            }

            return(customAlbum);
        }
コード例 #2
0
        void AddAlbum(object sender, EventArgs args)
        {
            var alertController = UIAlertController.Create("New Album", null, UIAlertControllerStyle.Alert);

            alertController.AddTextField(textField =>
            {
                textField.Placeholder = "Album Name";
            });

            alertController.AddAction(UIAlertAction.Create("Create", UIAlertActionStyle.Default, action =>
            {
                var textField = alertController.TextFields[0];

                var title = textField.Text;
                if (!string.IsNullOrEmpty(title))
                {
                    // Create a new album with the title entered.
                    PHPhotoLibrary.SharedPhotoLibrary.PerformChanges(() =>
                    {
                        PHAssetCollectionChangeRequest.CreateAssetCollection(title);
                    }, (success, error) =>
                    {
                        if (!success)
                        {
                            Console.WriteLine($"error creating album: {error}");
                        }
                    });
                }
            }));
            PresentViewController(alertController, true, null);
        }
コード例 #3
0
        void SetupAlbum()
        {
            var album = PHAssetCollection.FetchAssetCollections(new[] { Xamarin.Essentials.Preferences.Get("iOSAlbumIdentifier", string.Empty).ToString() }, null)?.firstObject as PHAssetCollection;

            if (album == null)
            {
                var albums = PHAssetCollection.FetchAssetCollections(PHAssetCollectionType.Album, PHAssetCollectionSubtype.Any, new PHFetchOptions
                {
                    IncludeAssetSourceTypes = PHAssetSourceType.UserLibrary
                });
                if (albums.Count > 0)
                {
                    foreach (var a in albums)
                    {
                        var collection = (a as PHAssetCollection);
                        if (collection.LocalizedTitle.Equals("ConferenceVision"))
                        {
                            SaveIdentifier(collection.LocalIdentifier);
                            return;
                        }
                    }
                }
                PHPhotoLibrary.SharedPhotoLibrary.PerformChanges(() =>
                {
                    var req         = PHAssetCollectionChangeRequest.CreateAssetCollection("ConferenceVision");
                    albumIdentifier = req.PlaceholderForCreatedAssetCollection.LocalIdentifier;
                    SaveIdentifier(albumIdentifier);
                }, (success, error) =>
                {
                    Debug.Write("Create Album " + success);
                });
            }
        }
コード例 #4
0
        partial void AddButtonClickHandler(NSObject sender)
        {
            // Prompt user from new album title.
            var alertController = UIAlertController.Create("New Album", null, UIAlertControllerStyle.Alert);

            alertController.AddTextField(textField => {
                textField.Placeholder = "Album Name";
            });

            alertController.AddAction(UIAlertAction.Create("Cancel", UIAlertActionStyle.Cancel, null));

            alertController.AddAction(UIAlertAction.Create("Create", UIAlertActionStyle.Default, action => {
                UITextField textField = alertController.TextFields.First();
                string title          = textField.Text;
                if (string.IsNullOrEmpty(title))
                {
                    return;
                }

                // Create a new album with the title entered.
                PHPhotoLibrary.SharedPhotoLibrary.PerformChanges(() =>
                                                                 PHAssetCollectionChangeRequest.CreateAssetCollection(title), (success, error) => {
                    if (!success)
                    {
                        Console.WriteLine(error.LocalizedDescription);
                    }
                });
            }));

            PresentViewController(alertController, true, null);
        }
コード例 #5
0
        public Task <IMediaAlbum> CreateAlbum(string title)
        {
            return(Task.Run(() =>
            {
                IMediaAlbum album = null;
                string id = null;
                if (PHPhotoLibrary.SharedPhotoLibrary.PerformChangesAndWait(() =>
                {
                    var request = PHAssetCollectionChangeRequest.CreateAssetCollection(title);
                    id = request.PlaceholderForCreatedAssetCollection.LocalIdentifier;
                }, out var error))
                {
                    var col = (PHAssetCollection)PHAssetCollection.FetchAssetCollections(new[] { id }, null).First();
                    album = new MediaAlbum(col);
                }

                return album;
            }));
        }
コード例 #6
0
        public static Task <PHAssetCollection> CreateAlbum(string name)
        {
            var source = new TaskCompletionSource <PHAssetCollection>();
            PHObjectPlaceholder placeholder = null;

            PHPhotoLibrary.SharedPhotoLibrary.PerformChanges(() =>
            {
                var request = PHAssetCollectionChangeRequest.CreateAssetCollection(name);
                placeholder = request.PlaceholderForCreatedAssetCollection;
            }, (success, err) =>
            {
                if (success)
                {
                    var result = PHAssetCollection.FetchAssetCollections(new[] { placeholder.LocalIdentifier }, null);
                    source.SetResult((PHAssetCollection)result.firstObject);
                }
                else
                {
                    source.SetException(new Exception(err.ToString()));
                }
            });
            return(source.Task);
        }
コード例 #7
0
ファイル: MyiOSInterface.cs プロジェクト: fEndman/WarFactory
        public async Task <string> ImageSave(MemoryStream stream, bool compatibleMode, string fileName = null)
        {
            NSError error = null;

            //虽然对于iOS没有这两个权限,但要保证方法异步,所以还是保留下来了
            await Permissions.RequestAsync <Permissions.StorageWrite>();

            await Permissions.RequestAsync <Permissions.StorageRead>();

            //判断相册是否存在,不存在就创建
            PHAssetCollection appAlbum = null;
            PHFetchResult     albums   = PHAssetCollection.FetchAssetCollections(PHAssetCollectionType.Album, PHAssetCollectionSubtype.Any, null);

            foreach (PHAssetCollection album in albums)
            {
                if (album.LocalizedTitle == albumName)
                {
                    appAlbum = album;
                }
            }
            if (appAlbum == null)   //相册不存在,新建
            {
                string[] albumID = new string[1];
                PHPhotoLibrary.SharedPhotoLibrary.PerformChangesAndWait(() =>
                {
                    albumID[0] = PHAssetCollectionChangeRequest.CreateAssetCollection(albumName).PlaceholderForCreatedAssetCollection.LocalIdentifier;
                }, out error);
                appAlbum = PHAssetCollection.FetchAssetCollections(albumID, null)[0] as PHAssetCollection;
            }

            //获取路径及名称
            string documentsPath;

            documentsPath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
            if (fileName == null || fileName == "")
            {
                fileName = "Tank_" + DateTime.Now.ToLocalTime().ToString("yyyyMMdd_HHmmss") + ".png";
            }
            string path = Path.Combine(documentsPath, fileName);

            //保存
            FileStream photoTankFile = new FileStream(path, FileMode.Create);

            byte[] photoTank = stream.ToArray();
            photoTankFile.Write(photoTank, 0, photoTank.Length);
            photoTankFile.Flush();
            photoTankFile.Close();

            //如果是图片或视频,就添加到相册里
            string MimeType = MimeUtility.GetMimeMapping(path);

            if (MimeType.IndexOf("image") != -1 || MimeType.IndexOf("video") != -1)
            {
                string[] assetID = new string[1];
                PHPhotoLibrary.SharedPhotoLibrary.PerformChangesAndWait(() =>
                {
                    if (MimeType.IndexOf("image") != -1)
                    {
                        assetID[0] = PHAssetChangeRequest.FromImage(new NSUrl(path, true)).PlaceholderForCreatedAsset.LocalIdentifier;
                    }
                    if (MimeType.IndexOf("video") != -1)
                    {
                        assetID[0] = PHAssetChangeRequest.FromVideo(new NSUrl(path, true)).PlaceholderForCreatedAsset.LocalIdentifier;
                    }
                }, out error);
                PHAsset    asset = PHAsset.FetchAssetsUsingLocalIdentifiers(assetID, null)[0] as PHAsset;
                PHObject[] objs  = { asset };
                PHPhotoLibrary.SharedPhotoLibrary.PerformChangesAndWait(() =>
                {
                    PHAssetCollectionChangeRequest collectionChangeRequest = PHAssetCollectionChangeRequest.ChangeRequest(appAlbum);
                    collectionChangeRequest.InsertAssets(objs, new NSIndexSet(0));
                }, out error);
            }

            return(Path.Combine(fileName));
        }