I've this function of getting pixels from an image written in Java. I'm converting it into Objective C. p1 is my object of Pixel class. Inside getpixels function, the first two lines shows how to get the image width and height and store it into p1.width and p1.height. The third line is meant to create a series of ints equal to number of pixels of image and store them into pixels array.(i.e. to create an int for every pixel of image)
public void getpixels(Bitmap image, Effect temp)
{
p1.width=image.getWidth();
p1.height=image.getHeight();
p1.pixels=new int[p1.width*p1.height];
}
I've converted the first two lines but I don't know how to convert the third one.
-(void) getpixels: (UIImage *) image :(Effect *) temp
{
p1.width= (int) image.size.width;
p1.height=(int) image.size.height;
int size= ([p1 width]*[p1 height]);
//array =(
}
Any help will be appreciated. Thanks.
If you want to dynamically add objects to the array you will have to create a mutable array:
NSMutableArray *array = [NSMutableArray arrayWithCapacity:[p1 width]*[p1 height]];
This will create a mutable array where you can add objects to. In objective C you don't create an int array or float array (you can write an int array in plain c if you want) instead you just create an array and you can then add whatever object you want as long as its an object and not a primitive.
To add an object to the array use:
[array addObject:someObj]
To add an int to the array you will have to wrap it with a NSNumber
so it will be an object and not a primitive, for example to add the primitive 31 int:
[array addObject:@(31)]
the @(31)
syntax is just an easy way to create an NSNumber
, it is equivalent to using [NSNumber numberWithInt:31]