Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

antago

macrumors newbie
Original poster
Nov 1, 2010
10
0
Right now I have a class that downloads data from a URL and store it in a variable named "data".

I have been using UIImage and UIImageView to display the data. Problem is, everytime I need to crop a region of it, I am running into some serious issues.

1) You can crop parts of the UIImage using CGImageCreateWithImageInRect, yet it is completely unproportional when viewed in UIImageView. I am not sure if it is UIImageView or UIImage's fault, but there appears to be a number of annoying responses on google that supposedly solve this issue, and yet they don't. One even extends the category of UIImage, which I feel is a little ridiculous and uncalled for.

Are there better ways of handling image data? If so, what is a very clean, efficient, and usable way to take data, display it, and then when I want to crop a part of its region to update the display with the new clip using the same object? In other words, the same UIImageView so that I don't have to recreate one every time.

Here's my code so far, which isn't working correctly. It sort of works, but then the cropped region is clipped seemingly to the previous proportions of the image. Thus, I am not getting a 32x32 image. I am getting a 32xwhatever image, height being clipped to match the previous aspect ratio.

Code:
	- ( void )process
	{
		UIImage* bitmap = [ [ UIImage alloc ] initWithData:data ];
		image = [ [ UIImageView alloc ] initWithImage:bitmap ];
		[ bitmap release ];
		
		[ self addSubview:image ];
		
		CGRect rect = CGRectMake( 0, 0, 32, 32 );
		[ self setImagePart:rect ];
	};
	
	- ( void )setImagePart:( CGRect )prect
	{
		UIImage* bitmap = [ [ UIImage alloc ] initWithData:data ];
		
		CGImageRef crop = CGImageCreateWithImageInRect( [ bitmap CGImage ], prect );
		[ image setImage:[ UIImage imageWithCGImage:crop ] ];
		CGImageRelease( crop );
		
		image.frame = prect;
		
		[ bitmap release ];
}
 
Okay, I found what is going on. The image is being cropped seemingly correctly. However, it is 20 pixels too short.

For instance, if I pass a CGrect of "CGRect rect = CGRectMake( 0, 0, 32.0, 52.0 );", it will indeed come up as 32x32.

However, if I want a rect of 32x32, it will crop it to 32x12

Why is this 20 pixels off on the height??
 
I figured it out. The title bar on the iPhone actually overlaps the window. It is 20 pixels tall. Because I was cropping to 32x32, it gave the illusion of being miscaled.

Apparently the code works absolutely fine. The title bar messed me up haha
 
Yeah, if you have the status bar overlapping your UI elements you should revisit your view hierarchy and how you build it. The main window should contain the status bar and then a main view into which all your elements are added.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.