Posted By

devb0yax on 07/12/10


Tagged


Versions (?)

Who likes this?

2 people have marked this snippet as a favorite

choise
bobbym245


draw image


 / Published in: iPhone
 

  1. MyDrawView file:
  2.  
  3. #import <UIKit/UIKit.h>
  4.  
  5.  
  6. @interface MyDrawView : UIView {
  7. CGPoint firstTouch;
  8. CGPoint lastTouch;
  9. UIImage *drawImage;
  10. CGRect redrawRect;
  11. }
  12.  
  13. @property CGPoint firstTouch;
  14. @property CGPoint lastTouch;
  15. @property (nonatomic, retain) UIImage *drawImage;
  16. @property (readonly) CGRect currentRect;
  17. @property CGRect redrawRect;
  18.  
  19. @end
  20.  
  21. -------------------------------------
  22. #import "MyDrawView.h"
  23.  
  24.  
  25. @implementation MyDrawView
  26.  
  27. @synthesize firstTouch;
  28. @synthesize lastTouch;
  29. @synthesize drawImage;
  30. @synthesize redrawRect;
  31. @synthesize currentRect;
  32.  
  33. -(CGRect)currentRect
  34. {
  35. return CGRectMake((firstTouch.x > lastTouch.x) ? lastTouch.x : firstTouch.x,
  36. (firstTouch.y > lastTouch.y) ? lastTouch.y : firstTouch.y,
  37. fabsf(firstTouch.x - lastTouch.x),
  38. fabsf(firstTouch.y - lastTouch.y));
  39. }
  40.  
  41. - (id)initWithCoder:(NSCoder*)coder
  42. {
  43. if (( self = [super initWithCoder:coder] ))
  44. {
  45. if (drawImage == nil)
  46. {
  47. self.drawImage = [UIImage imageNamed:@"green_circle.png"];
  48. }
  49. }
  50. return self;
  51. }
  52.  
  53. - (id)initWithFrame:(CGRect)frame {
  54. if ((self = [super initWithFrame:frame])) {
  55. // Initialization code
  56. }
  57. return self;
  58. }
  59.  
  60. - (void)drawRect:(CGRect)rect {
  61. // Drawing code
  62. CGFloat horizontalOffset = drawImage.size.width / 2;
  63. CGFloat verticalOffset = drawImage.size.height / 2;
  64. CGPoint drawPoint = CGPointMake(lastTouch.x - horizontalOffset, lastTouch.y - verticalOffset);
  65. [drawImage drawAtPoint:drawPoint];
  66. }
  67.  
  68. - (void)dealloc {
  69. [drawImage release];
  70. [super dealloc];
  71. }
  72.  
  73. #pragma mark -
  74. #pragma mark UI Touches
  75. #pragma mark -
  76. - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
  77. {
  78. UITouch *touch = [touches anyObject];
  79. firstTouch = [touch locationInView:self];
  80. lastTouch = [touch locationInView:self];
  81. [self setNeedsDisplay];
  82. }
  83.  
  84. - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
  85. {
  86. UITouch *touch = [touches anyObject];
  87. lastTouch = [touch locationInView:self];
  88. //[self setNeedsDisplay];
  89.  
  90. CGFloat horizontalOffset = drawImage.size.width / 2;
  91. CGFloat verticalOffset = drawImage.size.height / 2;
  92. redrawRect = CGRectUnion(redrawRect, CGRectMake(lastTouch.x - horizontalOffset,
  93. lastTouch.y - verticalOffset,
  94. drawImage.size.width,
  95. drawImage.size.height));
  96.  
  97. redrawRect = CGRectInset(redrawRect, -2.0, -2.0);
  98. [self setNeedsDisplayInRect:redrawRect];
  99. }
  100.  
  101. @end
  102.  
  103. ----------------------------
  104. MyDrawViewController
  105.  
  106. @interface MyDrawViewController : UIViewController {
  107. UIImageView *m_myImage;
  108. }
  109.  
  110. @property(nonatomic,retain) IBOutlet UIImageView *m_myImage;
  111.  
  112. @end
  113.  
  114. ------------
  115. .m file:
  116. ...
  117. - (void)viewDidLoad {
  118. [super viewDidLoad];
  119.  
  120. CGRect myImageRect = CGRectMake(0.0f, 0.0f, 320.0f, 480.0f);
  121. m_myImage = [[UIImageView alloc] initWithFrame:myImageRect];
  122. [m_myImage setImage:[UIImage imageNamed:@"Background.png"]];
  123. m_myImage.opaque = YES; // explicitly opaque for performance
  124. [self.view addSubview:m_myImage];
  125. }

Report this snippet  

You need to login to post a comment.