Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Francielle Castilhos 28 posts 150 karma points
    Sep 27, 2020 @ 23:59
    Francielle Castilhos
    0

    ImageCropper on IPublishedContent

    Hi,

    I have the following structure on my site:

    • Home
      • News
        • Year
          • Month
            • News Item

    On the homepage I have a partial that gets the 4 most recent news. The first is a 'highlighted' one that should have a 200x200 size, and the remaining 3 will have a 100x100 size. To manage the images, I have added a ImageCropper property. The user also has the option to overwrite the 'highlighted' one by using a content picker on the homepage and selecting which news will be highlighted. The issue is, I am not able to manage ImageCropper to work. Most of the examples I saw use Model.Image, but I'm not using Model, since I'm bringing the notes in a foreach. I also tried something like this:

     var foto = noticia.Value<IPublishedContent>("image").GetCropUrl(100,100);
    

    When using that, I get the following error: CS1503: Argument 1: cannot convert from 'method group' to 'HelperResult'

    I also tried:

    var foto = @(noticiaDestaque.Value<IPublishedContent>("imagemDestaque").GetCropUrl(100,100));
    

    Then I get: Value cannot be null. Parameter name: mediaItem Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

    Exception Details: System.ArgumentNullException: Value cannot be null. Parameter name: mediaItem Although the image is there.

    So I am a bit confused on what should be the correct approach. Is someone able to help me out?

    This is my code:

    @{
        IPublishedContent noticiaDestaque = Model.Value<IPublishedContent>("noticiaDestaque");
    }
        <div class="row">
                        @if (noticiaDestaque != null)
                        {
                        <div class="col-lg">
                            <a href="#" class="d-flex flex-column news news--highlight">
                                <div class="news__info">
                                    <p class="news__info__date">@(noticiaDestaque.Value<DateTime>("data").ToString("dd/MM/yyyy"))</p>
                                    <h4 class="news__info__title">@noticiaDestaque.Value("titulo")</h4>
                                </div>
                                @{
                        var foto =  noticiaDestaque.Value<IPublishedContent>("imagemDestaque").GetCropUrl(100,100);
                         }
    
                        <figure class="news__img text-center">
                            <img src="@foto" alt="">
                        </figure>
                                <figure class="news__img text-center">
                                    <img src="@foto" alt="">
                                </figure>
    
                                <div class="news__description">@Html.TruncateByWords(noticiaDestaque.Value("descricao").ToString(), 50, true)</div>
                            </a>
                        </div>
    
  • Steve Morgan 1346 posts 4453 karma points c-trib
    Sep 28, 2020 @ 08:28
    Steve Morgan
    0

    Hi,

    Use a typed media. Will need to check an image is picked.. this is written from memory but should help you.

    Something like:

    var fotoTyped = Umbraco.Media(noticia.Value<IPublishedContent>("image").Id );
    
    if(fotoTyped != null) {
       var foto = fotoTyped.GetCropUrl(100,100);
    }
    
  • Francielle Castilhos 28 posts 150 karma points
    Sep 28, 2020 @ 16:24
    Francielle Castilhos
    0

    Hey Steve

    I tried that and got the following:

    CS1503: Argument 1: cannot convert from 'method group' to 'Udi'

    I also tried changing it into two variables:

     var fotoID = noticiaDestaque.Value(imagemDestaque).Id;
     var fotoTyped = Umbraco.Media(fotoID);
    

    But got the following: CS0815: Cannot assign method group to an implicitly-typed variable

  • Steve Morgan 1346 posts 4453 karma points c-trib
    Sep 30, 2020 @ 10:02
    Steve Morgan
    0

    Hi,

    Just try writing out to the page the value of fotoTyped - it's hard to see what you have here and where your error is being thrown.

    You did replace

    var foto =  noticiaDestaque.Value<IPublishedContent>("imagemDestaque").GetCropUrl(100,100);
    

    with

        var fotoTyped = Umbraco.Media(noticia.Value<IPublishedContent>("image").Id );
    
    if(fotoTyped != null) {
       var foto = fotoTyped.GetCropUrl(100,100);
    }
    
Please Sign in or register to post replies

Write your reply to:

Draft