I have MjpegView which was downloaded from mjpeg-iphone (in mjpeg-iphone project, it is named as MJPEGClient, i renamed it to MjpegView for easier understanding of it's functionality)
Basically, MjpegView is extension of UIImageView and uses NSUrlConnection to fetch mjpeg images from url. I added UITapGestureRecognizer to MjpegView, but it is not responding when I tap.
Code snippet of MjpegView as below:
@interface MjpegView : UIImageView
@end
@implementation MjpegView
- (id)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
if (_endMarkerData == nil) {
uint8_t endMarker[2] = END_MARKER_BYTES;
_endMarkerData = [[NSData alloc] initWithBytes:endMarker length:2];
}
self.contentMode = UIViewContentModeScaleAspectFit;
singleTapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(onSingleTap:)];
singleTapRecognizer.numberOfTapsRequired = 1;
singleTapRecognizer.numberOfTouchesRequired = 1;
[self addGestureRecognizer:singleTapRecognizer];
[[NSURLConnection alloc] initWithRequest:[NSURLRequest requestWithURL:_url] delegate:self];
}
return self;
}
#pragma mark - NSURLConnection Delegate Methods
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
//NSLog(@"didReceiveResponse");
if (_receivedData) {
[_receivedData release];
}
_receivedData = [[NSMutableData alloc] init];
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
//NSLog(@"didReceiveData");
[_receivedData appendData:data];
NSRange endRange = [_receivedData rangeOfData:_endMarkerData
options:0
range:NSMakeRange(0, _receivedData.length)];
long long endLocation = endRange.location + endRange.length;
if (_receivedData.length >= endLocation) {
NSData *imageData = [_receivedData subdataWithRange:NSMakeRange(0, endLocation)];
UIImage *receivedImage = [UIImage imageWithData:imageData];
if (receivedImage) {
self.image = receivedImage;
}
}
}
@end
I suspect that is because the main thread is busy updating the view with mpjeg images and thus unable to detect the tap. Anyone has suggestion on how to implement this?
By default UIImageView
has userInteractionEnabled
is false
Set the property userInteractionEnabled= YES
Try this,
@implementation MjpegView
- (id)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
if (_endMarkerData == nil) {
uint8_t endMarker[2] = END_MARKER_BYTES;
_endMarkerData = [[NSData alloc] initWithBytes:endMarker length:2];
}
self.contentMode = UIViewContentModeScaleAspectFit;
self.userInteractionEnabled=YES;
singleTapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(onSingleTap:)];
singleTapRecognizer.numberOfTapsRequired = 1;
singleTapRecognizer.numberOfTouchesRequired = 1;
[self addGestureRecognizer:singleTapRecognizer];
[[NSURLConnection alloc] initWithRequest:[NSURLRequest requestWithURL:_url] delegate:self];
}
return self;
}