获取IOS SaveToPhotoAlbum的状态

时间:2016-05-30 04:06:03

标签: c# xamarin xamarin.ios uiimage

是否可以获取UIImage函数SaveToPhotoAlbum函数的状态?

我试图在保存照片后尽快分享照片,我需要知道照片何时保存到相册中。

我得到的最接近的例子是Share screenshot on Facebookxamarin ios screenshot and email

我尝试用电子邮件示例来做,但我得到的图像是白色的,因为我相信我在尝试保存到相册/保存框架之前尝试获取UIImage。

这就是我保存屏幕截图的方式

    public void ScreenGrab()
    {
        UIGraphics.BeginImageContext (UIApplication.SharedApplication.KeyWindow.RootViewController.View.Frame.Size);

        //new iOS 7 method to snapshot
        UIApplication.SharedApplication.KeyWindow.RootViewController.View.DrawViewHierarchy (UIApplication.SharedApplication.KeyWindow.RootViewController.View.Frame, true);

        UIImage image = UIGraphics.GetImageFromCurrentImageContext ();
        UIGraphics.EndImageContext ();

        //saves captured frame onto album
        image.SaveToPhotosAlbum((SavedImage, error) => {
            if(error ==null)
            {
                Console.WriteLine("error:" + error );
            }
        });

    }

这就是我获取最新截图的方式

public UIImage GetLatestImageFromAlbum()
    {
            UIImage image = new UIImage ();
            //must return a UIImage

            //Fetch all the assets based on the format given
            var assets = PHAsset.FetchAssets (new PHFetchOptions (){ Predicate = NSPredicate.FromFormat (@"mediaType == " + (int)PHAssetMediaType.Image) });

            //Request Options for the Image
            var options = new PHImageRequestOptions () {
                NetworkAccessAllowed = true,
                DeliveryMode = PHImageRequestOptionsDeliveryMode.HighQualityFormat,
                Synchronous = true
            };

            //Must check if there is access for the photo album on iOS
            if (PHPhotoLibrary.AuthorizationStatus == PHAuthorizationStatus.Authorized) {
                Console.WriteLine ("Access Granted");
                PHImageManager.DefaultManager.RequestImageForAsset ((PHAsset)assets.LastObject, UIApplication.SharedApplication.KeyWindow.RootViewController.View.Bounds.Size, PHImageContentMode.AspectFill, options, (UIImage result, NSDictionary info) => {
                    image = result;
                });
            } else {
                Console.WriteLine ("Access Denied");
            }



        return image;
    }

我在错误!= null时尝试调用GetLatestImageFromAlbum(),但它没有工作,因为我认为照片尚未完全保存,因此无法检索照片。

我也尝试在ScreenGrab()之后直接调用GetLatestImageFromAlbum(),但同样的事情也发生了,就像这样。

testIOS.ScreenGrab();
slComposer.AddImage(testIOS.GetLatestImageFromAlbum());

2 个答案:

答案 0 :(得分:0)

我不知道你想用哪种语言回答,但是在Swift 2.2中

UIImageWriteToSavedPhotosAlbum(image, self, #selector(self.imageSaved(_:didFinishSavingWithError:contextInfo:)), nil)

func imageSaved(image: UIImage!, didFinishSavingWithError error: NSError?, contextInfo: AnyObject?) {
    if (error != nil) {
        // Something wrong happened.
    } else {
        // Everything is alright.
    }
}

答案 1 :(得分:0)

我找到了解决问题的方法,首先我们必须触发screengrab然后在更新循环中等待检查​​图像是否已完成保存我使用布尔值完成,一旦完成,然后触发GrabLatestImageFromAlbum。