我有从mjpeg-iphone下载的MjpegView(在mjpeg-iphone项目中,它被命名为MJPEGClient,我将其重命名为MjpegView以便更容易理解它的功能)
基本上,MjpegView是UIImageView的扩展,并使用NSUrlConnection从url获取mjpeg图像。我将UITapGestureRecognizer添加到MjpegView,但是当我点击时它没有响应。
MjpegView的代码片段如下:
@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
我怀疑是因为主线程忙于用mpjeg图像更新视图,因此无法检测到点击。有人建议如何实现这个吗?
答案 0 :(得分:0)
默认情况下,UIImageView
userInteractionEnabled
为假
设置属性userInteractionEnabled= YES
试试这个,
@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;
}