iPhone 4 中 IOS 7 中奇怪的 UIScrollview 行为
Posted
技术标签:
【中文标题】iPhone 4 中 IOS 7 中奇怪的 UIScrollview 行为【英文标题】:Weird UIScrollview behaviour in IOS 7 in IPhone 4 【发布时间】:2014-06-20 12:37:02 【问题描述】:我正在开发一个与视频相关的应用程序。为此,我使用了AVFoundation
框架来捕获图像和视频。我正在捕获图像并在下一个视图中显示捕获的图像,其中图像视图是subview
到scrollview
。它在 iPhone 5、iPhone 5s 和 iPad 中运行良好,但在 iPhone 4 中捕获图像并附加到 scrollView
后,应用程序变得缓慢。滚动视图不像在其他设备中那样平滑滚动。我没有遇到错误的问题。我正在使用以下代码来捕获图像:
-(void)capturePhoto
_ciContext = [CIContext contextWithOptions:nil];
AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
// - input
AVCaptureDeviceInput *deviceInput = [AVCaptureDeviceInput deviceInputWithDevice:device error:NULL];
NSError *error = nil;
if ([device lockForConfiguration:&error])
if ([device isFlashModeSupported:AVCaptureFlashModeOff])
device.flashMode = AVCaptureFlashModeOff;
[device unlockForConfiguration];
// - output
_dataOutput = [[AVCaptureStillImageOutput alloc] init];
NSDictionary *outputSettings = [[NSDictionary alloc] initWithObjectsAndKeys:
AVVideoCodecJPEG, AVVideoCodecKey, nil];
_dataOutput.outputSettings = outputSettings;
// - output
NSMutableDictionary *settings;
settings = [NSMutableDictionary dictionary];
[settings setObject:[NSNumber numberWithInt:kCVPixelFormatType_32BGRA]
forKey:(__bridge id) kCVPixelBufferPixelFormatTypeKey];
_dataOutputVideo = [[AVCaptureVideoDataOutput alloc] init];
_dataOutputVideo.videoSettings = settings;
[_dataOutputVideo setSampleBufferDelegate:self queue:dispatch_get_main_queue()];
_session = [[AVCaptureSession alloc] init];
[_session addInput:deviceInput];
[_session addOutput:_dataOutput];
[_session addOutput:_dataOutputVideo];
// _session.sessionPreset = AVCaptureSessionPresetPhoto;
_session.sessionPreset = AVCaptureSessionPresetHigh;
// _session.sessionPreset = AVCaptureSessionPresetMedium;
[_session startRunning];
// add gesture
// UIGestureRecognizer *gr = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(didTapGesture:)];
// gr.delegate = self;
// [self.touchView addGestureRecognizer:gr];
_focusView = [[UIView alloc] init];
CGRect imageFrame = _focusView.frame;
imageFrame.size.width = 80;
imageFrame.size.height = 80;
_focusView.frame = imageFrame;
_focusView.center = CGPointMake(160, 202);
CALayer *layer = _focusView.layer;
layer.shadowOffset = CGSizeMake(2.5, 2.5);
layer.shadowColor = [[UIColor blackColor] CGColor];
layer.shadowOpacity = 0.5;
layer.borderWidth = 2;
layer.borderColor = [UIColor yellowColor].CGColor;
[self.touchView addSubview:_focusView];
_focusView.alpha = 0;
_isShowFlash = NO;
[self.view bringSubviewToFront:self.touchView];
UIView *footerView = [self.view viewWithTag:2];
[self.view bringSubviewToFront:footerView];
稍后我将像这样附加到滚动视图:
scrollImgView=[[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 320, 340)];
UIImage *image = [UIImage imageWithData:appdelegate.capturedImgData];
UIImage *tempImage=[self resizeImage:image withWidth:320 withHeight:340];
NSData *imgData=UIImageJPEGRepresentation(tempImage,1.0);//0.25f
NSLog(@"image is %@",image);
scrollImgView.image=[UIImage imageWithData:imgData];
// scrollImgView.contentMode = UIViewContentModeScaleAspectFit;
// UIViewContentModeScaleAspectFit;
[postScrollView addSubview:scrollImgView];
如果有人遇到同样的问题,请给我建议。
【问题讨论】:
【参考方案1】:你的编码没问题,设备也没有问题,可能会发生
1. network problem
2. device memory is already loaded fully.
3. some Data conversation also taken times
这里
UIImage *image = [UIImage imageWithData:appdelegate.capturedImgData];
UIImage *tempImage=[self resizeImage:image withWidth:320 withHeight:340]; //
NSData *imgData=UIImageJPEGRepresentation(tempImage,1.0);//0.25f
the above code u having used
1. data conversion is taken high and also taken the 2 time of image conversion optimize any `NSData`
2. third line is improve the `quality` of your image-- this also take the time for image conversion.
在我的建议中
1. use `Asychronous method`
-- sdwebimage
-- Asychronous Imageview
-- NSOperation_Queue
-- Dispatch im main_queue
使用其中的任何一个,它将为您获取一些响应。我的建议是使用SDWebImage
.. 使用此链接Loading takes a while when i set UIImage to a NSData with a url.
【讨论】:
【参考方案2】:我通过改变解决了这个问题
_session.sessionPreset = AVCaptureSessionPresetHigh
到
_session.sessionPreset = AVCaptureSessionPresetMedium
【讨论】:
以上是关于iPhone 4 中 IOS 7 中奇怪的 UIScrollview 行为的主要内容,如果未能解决你的问题,请参考以下文章
iphone中奇怪的UISearchBar取消按钮行为(在模拟器中工作正常)