2

I want to process a subregion of a UIImage in an iOS app. Following this question, I now have a routine to extract the region in question as a UIImage that I can now manipulate. Is there a similarly convenient method for placing the region back into the original image? The alternative I'm considering is a bytewise copy, which seems extremely low-level to me.

Community
  • 1
  • 1
mmr
  • 14,781
  • 29
  • 95
  • 145

1 Answers1

4

You could draw the two images on top of each other, and then combine them to one image.

Assuming you have the original image and the modified part:

UIGraphicsBeginImageContext(originalImage.size);

[originalImage drawAtPoint:CGPointMake(0, 0)];
[modifiedPart drawAtPoint:/* Upper left corner of the modified part */];
UIImage *combined = UIGraphicsGetImageFromCurrentImageContext();

Edit: Forgot this line:

UIGraphicsEndImageContext();
Hjalmar
  • 989
  • 8
  • 16
  • This approach shows promise, but it is not rotation independent. The chunks aren't being drawn in the places I'd expect, to put it mildly. – mmr Jan 03 '13 at 21:43
  • @mmr: I'm not sure what you mean with rotation independent. When you draw the modified part (which I assume still has the same size) you should provide the same coordinates as you used when chopping it. – Hjalmar Jan 04 '13 at 01:13
  • @vaderkvam-- I mean that if I rotate the phone (or simulator), I get extremely strange results. I'm investigating further, because I may also have gotten my initial code wrong as well. – mmr Jan 04 '13 at 01:18
  • @mmr: I see, well that has never happend to me using this code. The combined image behaves - with respect to rotation - the same way as any other image. Anyway good luck with the investigation! – Hjalmar Jan 04 '13 at 01:24
  • @vaderkvam-- the rotation problem was upstream. This code works, thanks! – mmr Jan 04 '13 at 23:39