2

So I looked at the examples on here for Obj-c on how to do this but when I do it in Xamarin I seem to be getting null for my image.

Here is the code

base.ViewDidLoad ();

    cropperView = new CropperView ();
    View.AddSubview (cropperView);

    NSObject notification;

    MPMoviePlayerController movie = new MPMoviePlayerController (url);

    NSNumber[] time;

    time = new NSNumber[1];

    time [0] = new NSNumber (1f);

    movie.RequestThumbnails (time, MPMovieTimeOption.Exact);

    notification = MPMoviePlayerController.Notifications.ObserveThumbnailImageRequestDidFinish ((sender, args) => {
        imgImage.Image = args.Image;
    });

The args.Image is the one that keeps returning null.

Edit: I haved edited the code and is now working.

Valener
  • 51
  • 1
  • 8
  • There's not enough code/context to be sure. Can you add the link to the ObjC code you ported? Also I'm not sure what is your `CropperView` but it's unlikely that it can be initialized like this: cropperView = new CropperView (new IntPtr()); The constructors that accept an `IntPtr` are meant to be given a valid handle (from a native instance). In general what you create from C# does not call this constructor. You call the default (parameter-less) constructor and an handle will be assigned (in native code). – poupou Jul 25 '14 at 18:35
  • @poupou Okay I will the change the intptr thing right now. And the code i ported over would be this http://stackoverflow.com/questions/4199879/iphone-read-uiimage-frames-from-video-with-avfoundation Also cropperView is just a view that i drew a rectangle in it. – Valener Jul 25 '14 at 18:47

2 Answers2

4

I used the following code to create the thumbnail from video and it works perfectly

public static UIImage GenerateImage(NSUrl videoUrl)
{
    var asset = AVAsset.FromUrl(videoUrl);
    var imageGenerator = AVAssetImageGenerator.FromAsset(asset);
    imageGenerator.AppliesPreferredTrackTransform = true;

    var actualTime = asset.Duration;
    CoreMedia.CMTime cmTime = new CoreMedia.CMTime(1, 60);

    NSError error;
    var imageRef = imageGenerator.CopyCGImageAtTime(cmTime, out actualTime, out error);

    if (imageRef == null)
        return null;

    var image = UIImage.FromImage(imageRef);

    return image;
}
Kurt Van den Branden
  • 11,995
  • 10
  • 76
  • 85
Fahad Rehman
  • 1,189
  • 11
  • 25
0

I found the answer. I had to change the new NSNumber(1) to be a float for new NSNumber(1f) and it worked fine afterwards.

Valener
  • 51
  • 1
  • 8