Friday, August 24, 2018

React Router - Route With Optional Params Not Working

I am using react and react-router. In my react router configuration, I want to add a route that will accept the following URL: http://localhost:7000/example/GUID where GUID is the user's GUID that is logged in at that moment.

I am using WebPack.

I went to the react-router documentation: https://github.com/ReactTraining/react-router/blob/master/docs/guides/RouteMatching.md#path-syntax

But that does not work for some reason, maybe I am missing something.

This is my react router configuration:

import React from 'react';
import { Router, Route, IndexRoute, browserHistory } from "react-router";
import Main from "../containers/main.js";
import Home from "../containers/home.js";
import Example from "../containers/exampleComponent.js";

var Routes = (
    
        
            
            
        
    
);

module.exports = Routes;

When I go to http://localhost:7000/example, it works fine, it loads the example component.

But When I go to http://localhost:7000/example/GUID, it does not work. In fact, it does not load anything then. According to the documentation, the "()" in the route path indicates that it is optional.

Is there anything else that I need to do to make this work? I've been scratching my head on this one...

EDIT: Please see my github project here: https://github.com/FredM7/react-base The project includes the problem.

Solved

i am also using path like this path: 'cn/*/cid/(:catZeroId)(/:catOneId)(/:catTwoId)' it works flawlessly for me.

you might be having react-router version issue, i am using "react-router": "^3.0.0", "react-router-redux": "^4.0.6",

or you can create https://jsbin.com/, https://plnkr.co/ to simulate your case.


I was going to say you need to add a


tag inside the head tags of your base HTML page.

But I just looked at your code and you've already done that.


Monday, August 20, 2018

object emptied in foreach loop [duplicate]

This question already has an answer here:

I have a list being passed into a foreach loop which removes 0 or more items from the list. Based on conditions it is possible for the list to be entirely emptied which causes a null reference error. What is the best way to handle this sort of situation?

foreach (Item i in items)
{
    if (i.property == condition)
    {
        items.Remove(i);
    }
}

Solved

Use List.RemoveAll method

items.RemoveAll(i => i.property == condition)

It removes all the elements that match the conditions defined by the specified predicate.

You also can leave original list (or another collection) untouched by creating new list without items which match condition (sometimes that might be useful)

var newItems = items.Where(i => i.property != condition).ToList();

Sunday, August 19, 2018

Generate 16-bit grayscale BitmapData and save to file

I am trying to generate 16bit grayscale Bitmap in C# from a random data.But it crashed on Marshal.Copy.

Here is my code:

   Bitmap b16bpp;
    private void GenerateDummy16bitImage()
    {

        b16bpp = new Bitmap(IMAGE_WIDTH, IMAGE_HEIGHT, System.Drawing.Imaging.PixelFormat.Format16bppGrayScale);

        var rect = new Rectangle(0, 0, IMAGE_WIDTH, IMAGE_HEIGHT);
        var bitmapData = b16bpp.LockBits(rect, ImageLockMode.WriteOnly, b16bpp.PixelFormat);
        // Calculate the number of bytes required and allocate them.
        var numberOfBytes = bitmapData.Stride * IMAGE_HEIGHT * 2;
        var bitmapBytes = new short[numberOfBytes];
        // Fill the bitmap bytes with random data.
        var random = new Random();
        for (int x = 0; x < IMAGE_WIDTH; x++)
        {
            for (int y = 0; y < IMAGE_HEIGHT; y++)
            {

                var i = ((y * IMAGE_WIDTH) + x) * 2; // 16bpp

                // Generate the next random pixel color value.
                var value = (short)random.Next(5);

                bitmapBytes[i] = value;         // BLUE
                bitmapBytes[i + 1] = value;     // GREEN
                bitmapBytes[i + 2] = value;     // RED
              //  bitmapBytes[i + 3] = 0xFF;      // ALPHA
            }
        }
        // Copy the randomized bits to the bitmap pointer.
        var ptr = bitmapData.Scan0;
        Marshal.Copy(bitmapBytes, 0, ptr, numberOfBytes);//crashes here

        // Unlock the bitmap, we're all done.
        b16bpp.UnlockBits(bitmapData);

        b16bpp.Save("random.bmp", ImageFormat.Bmp);
        Debug.WriteLine("saved");
    }

The exception is:

An unhandled exception of type 'System.AccessViolationException' occurred in mscorlib.dll

This is not my code.I found it in relation to 32bit Bitmaps and modified.But I guess I have missed something as I am pretty new to C#.

Basically,all I need is to wrap into BitmapData an arrays of shorts.

Solved

I have corrected some of your mistakes (mostly wrong sizes). But it will still crash on b16bpp.Save(), because GDI+ does not support saving 16bit grayscale images.

Bitmap b16bpp;
private void GenerateDummy16bitImage()
{

    b16bpp = new Bitmap(IMAGE_WIDTH, IMAGE_HEIGHT, System.Drawing.Imaging.PixelFormat.Format16bppGrayScale);

    var rect = new Rectangle(0, 0, IMAGE_WIDTH, IMAGE_HEIGHT);
    var bitmapData = b16bpp.LockBits(rect, ImageLockMode.WriteOnly, b16bpp.PixelFormat);
    // Calculate the number of bytes required and allocate them.
    var numberOfBytes = bitmapData.Stride * IMAGE_HEIGHT;
    var bitmapBytes = new short[IMAGE_WIDTH * IMAGE_HEIGHT];
    // Fill the bitmap bytes with random data.
    var random = new Random();
    for (int x = 0; x < IMAGE_WIDTH; x++)
    {
        for (int y = 0; y < IMAGE_HEIGHT; y++)
        {

            var i = ((y * IMAGE_WIDTH) + x); // 16bpp

            // Generate the next random pixel color value.
            var value = (short)random.Next(5);

            bitmapBytes[i] = value;         // GRAY
        }
    }
    // Copy the randomized bits to the bitmap pointer.
    var ptr = bitmapData.Scan0;
    Marshal.Copy(bitmapBytes, 0, ptr, bitmapBytes.Length);

    // Unlock the bitmap, we're all done.
    b16bpp.UnlockBits(bitmapData);

    b16bpp.Save("random.bmp", ImageFormat.Bmp);
    Debug.WriteLine("saved");
}

Explanation of my changes:

  • bitmapData.Stride is already IMAGE_WIDTH * BytesPerPixel so you don't need to multiply by 2
  • as you declared bitmapBytes as short[] it has to have the size of the image in pixels not in bytes
  • that means you also do not need to multiply i by 2
  • since you have a grayscale image it does not have a blue, green and red channel, but one single 16bit gray channel
  • Marshal.Copy takes the length in "array units" not in bytes

All in all you tried to copy an array 8 times to large into the bitmap.


This works for System.Drawing.Imaging.PixelFormat.Format16bppGrayScale:

    private static void SaveBmp(Bitmap bmp, string path)
    {
        Rectangle rect = new Rectangle(0, 0, bmp.Width, bmp.Height);

        BitmapData bitmapData = bmp.LockBits(rect, ImageLockMode.ReadOnly, bmp.PixelFormat);

        var pixelFormats = ConvertBmpPixelFormat(bmp.PixelFormat);

        BitmapSource source = BitmapSource.Create(bmp.Width,
                                                  bmp.Height,
                                                  bmp.HorizontalResolution,
                                                  bmp.VerticalResolution,
                                                  pixelFormats,
                                                  null,
                                                  bitmapData.Scan0,
                                                  bitmapData.Stride * bmp.Height,
                                                  bitmapData.Stride);

        bmp.UnlockBits(bitmapData);


        FileStream stream = new FileStream(path, FileMode.Create);

        TiffBitmapEncoder encoder = new TiffBitmapEncoder();

        encoder.Compression = TiffCompressOption.Zip;
        encoder.Frames.Add(BitmapFrame.Create(source));
        encoder.Save(stream);

        stream.Close();
    }

    private static System.Windows.Media.PixelFormat ConvertBmpPixelFormat(System.Drawing.Imaging.PixelFormat pixelformat)
    {
        System.Windows.Media.PixelFormat pixelFormats = System.Windows.Media.PixelFormats.Default;

        switch (pixelformat)
        {
            case System.Drawing.Imaging.PixelFormat.Format32bppArgb:
                pixelFormats = PixelFormats.Bgr32;
                break;

            case System.Drawing.Imaging.PixelFormat.Format8bppIndexed:
                pixelFormats = PixelFormats.Gray8;
                break;

            case System.Drawing.Imaging.PixelFormat.Format16bppGrayScale:
                pixelFormats = PixelFormats.Gray16;
                break;
        }

        return pixelFormats;
    }

Saturday, August 18, 2018

iTunes connect upload issue (binary size)

I am trying to upload an update to an existing app I have in the appstore. I created an archive, passed validation and submitted the file via the Application Loader.

At the end of the upload I got the following message :

The resulting API analysis is too large when upload app to mac store

Have no idea what it means and found some sources saying this is just a warning and it will not be rejected.

Looking in the build details of my uploaded build in the new itunes connect page I noticed that the file size is much smaller than the generated IPA file created by Xcode (2.69MB against 12.9MB)

And one last thing, under the Processing tab my uploaded build appears 4 times with status 'Created' for each.

Did anyone bumped upon these issues above? I have a bad feeling that my build will be rejected for "Invalid Binary"...

Solved

Ok, so I contacted Apple support and they said they'll look into it... A few days after the file size in iTunesConnect has been updated to the right size and the app got approved a way long after;)

So apparently it was a glitch on the site so if some of you notice this, either wait a few days to be updated or contact the support (but don't try to reject and upload again as it will probably happen again until they fix it)