0

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.

nobody
  • 19,814
  • 17
  • 56
  • 77
user3863537
  • 101
  • 1
  • 12
  • See [NSArray](https://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSArray_Class/NSArray.html) or a subclass such as [NSMutableArray](https://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSMutableArray_Class/Reference/Reference.html) if you need to modify the array after creating it. – nobody Aug 22 '14 at 13:02
  • you have several choices for arrays. For easy objective c, look at `NSArray`. For super-low-level (fast), look into `malloc` and `free`. To eventually get a UIImage, you will likely get faster results by going via CGImage (which is more low-level) with a malloced array of RGBA pixel values. – Dave Aug 22 '14 at 13:02
  • I actually need to know whether I can allocate size to an array and then put it equal to p1.pixels or not? (done in java code) – user3863537 Aug 22 '14 at 13:06

1 Answers1

2

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]

Eyal
  • 10,777
  • 18
  • 78
  • 130