
我没有使用snapshotVIEwAfterScreenUpdates.
以下是我到目前为止的代码:
UIImage *snapshotimage;/* draw the image of all the vIEws below our vIEw */UIGraphicsBeginImageContextWithOptions(self.pendantimageVIEw.bounds.size,NO,0);BOol successfulDrawHIErarchy = [self.pendantimageVIEw drawVIEwHIErarchyInRect:self.pendantimageVIEw.bounds afterScreenUpdates:YES];if ( successfulDrawHIErarchy ) { snapshotimage = UIGraphicsGetimageFromCurrentimageContext();} else { NSLog(@"drawVIEwHIErarchyInRect:afterScreenUpdates: Failed - there's nothing to draw...");}UIGraphicsEndImageContext();UIImage *croppedImage;if ( successfulDrawHIErarchy ) { /* calculate the coordinates of the rectangle we're interested in within the returned image */ CGRect cropRect = CGRectOffset(pendantFrame,- self.pendantimageVIEw.frame.origin.x,- self.pendantimageVIEw.frame.origin.y); /* draw the cropped section with a clipPing region */ UIGraphicsBeginImageContextWithOptions(cropRect.size,YES,0); CGContextRef context = UIGraphicsGetCurrentContext(); CGContextClipToRect(context,CGRectMake(0,cropRect.size.wIDth,cropRect.size.height)); CGRect targetRectangeForCrop = CGRectMake(-cropRect.origin.x,-cropRect.origin.y,snapshotimage.size.wIDth,snapshotimage.size.height); [snapshotimage drawInRect:targetRectangeForCrop]; croppedImage = UIGraphicsGetimageFromCurrentimageContext(); UIGraphicsEndImageContext(); } pendantimageVIEw是底部的imageVIEw,pendantFrame是我正在尝试裁剪的区域的中间坐标.
提前致谢!
解决方法 我不知道我是否正确理解你,但这是我的结果:(点击视频观看完整版)
- (voID)vIEwWillAppear:(BOol)animated{ [super vIEwWillAppear:animated]; UIImageVIEw *theImageVIEw = [UIImageVIEw new]; [self.vIEw addSubvIEw:theImageVIEw]; theImageVIEw.image = [UIImage imagenamed:@"image1.jpg"]; theImageVIEw.frame = self.vIEw.frame; theImageVIEw.userInteractionEnabled = YES; theImageVIEw.Alpha = 0.6; UIImageVIEw *thePanImageVIEw = [UIImageVIEw new]; [self.vIEw addSubvIEw:thePanImageVIEw]; thePanImageVIEw.frame = CGRectMake(0,100,100); thePanImageVIEw.center = CGPointMake(theImageVIEw.frame.size.wIDth/2,theImageVIEw.frame.size.height/2); thePanImageVIEw.image = [self screenshotFromrect:thePanImageVIEw.frame fromVIEw:theImageVIEw]; thePanImageVIEw.userInteractionEnabled = YES; thePanImageVIEw.layer.cornerRadius = thePanImageVIEw.frame.size.wIDth/2; thePanImageVIEw.clipsToBounds = YES; thePanImageVIEw.theWeakObject = theImageVIEw; { UIPanGestureRecognizer *thePanGesture = [UIPanGestureRecognizer new]; [thePanGesture addTarget:self action:@selector(handlePanGesture:)]; [thePanImageVIEw addGestureRecognizer:thePanGesture]; UIPinchGestureRecognizer *thePinchGesture = [UIPinchGestureRecognizer new]; [thePinchGesture addTarget:self action:@selector(handlePinchGesture:)]; [thePanImageVIEw addGestureRecognizer:thePinchGesture]; }}- (voID)handlePanGesture:(UIPanGestureRecognizer *)thePanGesture{ UIImageVIEw *thePanImageVIEw = (ID)thePanGesture.vIEw; UIImageVIEw *theImageVIEw = thePanImageVIEw.theWeakObject; thePanImageVIEw.center = [thePanGesture locationInVIEw:theImageVIEw]; thePanImageVIEw.image = [self screenshotFromrect:thePanImageVIEw.frame fromVIEw:theImageVIEw];}- (voID)handlePinchGesture:(UIPinchGestureRecognizer *)thePinchGesture{ UIImageVIEw *thePanImageVIEw = (ID)thePinchGesture.vIEw; static CGRect theInitialFrame; if (thePinchGesture.state == UIGestureRecognizerStateBegan) { theInitialFrame = thePanImageVIEw.frame; } else { CGRect theFrame = theInitialFrame; theFrame.size.wIDth *= thePinchGesture.scale; theFrame.size.height *= thePinchGesture.scale; thePanImageVIEw.frame = theFrame; } thePanImageVIEw.layer.cornerRadius = thePanImageVIEw.frame.size.wIDth/2; UIImageVIEw *theImageVIEw = thePanImageVIEw.theWeakObject; thePanImageVIEw.center = [thePinchGesture locationInVIEw:theImageVIEw]; thePanImageVIEw.image = [self screenshotFromrect:thePanImageVIEw.frame fromVIEw:theImageVIEw];}- (UIImage * __nonnull)screenshotFromrect:(CGRect)theRect fromVIEw:(UIVIEw * __nonnull)theVIEw;{ if (!theVIEw) { abort(); } if (theRect.size.height < 1 || theRect.size.wIDth < 1) { abort(); } if ([[UIScreen mainScreen] respondsToSelector:@selector(scale)]) { UIGraphicsBeginImageContextWithOptions(theRect.size,[UIScreen mainScreen].scale); } else { UIGraphicsBeginImageContext(theRect.size); } CGContextRef ctx = UIGraphicsGetCurrentContext(); CGContextTranslateCTM(ctx,-theRect.origin.x,-theRect.origin.y); [theVIEw.layer renderInContext:ctx]; UIImage *snapshotimage = UIGraphicsGetimageFromCurrentimageContext(); UIGraphicsEndImageContext(); return snapshotimage;} 编辑:
上述解决方案在cpu方面效率低下,因为它每次移动视图时都需要截图.
一个更有效的方式是创建一个额外的UIImageVIEw,并将其移动到您的PangImageVIEw内
代码如下:
- (voID)vIEwWillAppear:(BOol)animated{ [super vIEwWillAppear:animated]; UIImageVIEw *theImageVIEw = [UIImageVIEw new]; [self.vIEw addSubvIEw:theImageVIEw]; theImageVIEw.image = [UIImage imagenamed:@"image1.jpg"]; theImageVIEw.frame = self.vIEw.frame; theImageVIEw.userInteractionEnabled = YES; theImageVIEw.Alpha = 0.6; UIImageVIEw *thePanImageVIEw = [UIImageVIEw new]; [self.vIEw addSubvIEw:thePanImageVIEw]; thePanImageVIEw.frame = CGRectMake(0,theImageVIEw.frame.size.height/2); thePanImageVIEw.userInteractionEnabled = YES; thePanImageVIEw.layer.cornerRadius = thePanImageVIEw.frame.size.wIDth/2; thePanImageVIEw.clipsToBounds = YES; thePanImageVIEw.layer.bordercolor = [UIcolor redcolor].CGcolor; thePanImageVIEw.layer.borderWIDth = 1; thePanImageVIEw.theWeakObject = theImageVIEw; { UIPanGestureRecognizer *thePanGesture = [UIPanGestureRecognizer new]; [thePanGesture addTarget:self action:@selector(handlePanGesture:)]; [thePanImageVIEw addGestureRecognizer:thePanGesture]; UIPinchGestureRecognizer *thePinchGesture = [UIPinchGestureRecognizer new]; [thePinchGesture addTarget:self action:@selector(handlePinchGesture:)]; [thePanImageVIEw addGestureRecognizer:thePinchGesture]; UIImageVIEw *theExtraimageVIEw = [UIImageVIEw new]; [thePanImageVIEw addSubvIEw:theExtraimageVIEw]; theExtraimageVIEw.frame = CGRectMake(-thePanImageVIEw.frame.origin.x,-thePanImageVIEw.frame.origin.y,theImageVIEw.frame.size.wIDth,theImageVIEw.frame.size.height); theExtraimageVIEw.image = theImageVIEw.image; }}- (voID)handlePanGesture:(UIPanGestureRecognizer *)thePanGesture{ UIImageVIEw *thePanImageVIEw = (ID)thePanGesture.vIEw; UIImageVIEw *theImageVIEw = thePanImageVIEw.theWeakObject; thePanImageVIEw.center = [thePanGesture locationInVIEw:theImageVIEw]; UIImageVIEw *theExtraimageVIEw = thePanImageVIEw.subvIEws.firstObject; theExtraimageVIEw.frame = CGRectMake(-thePanImageVIEw.frame.origin.x,theExtraimageVIEw.frame.size.wIDth,theExtraimageVIEw.frame.size.height);}- (voID)handlePinchGesture:(UIPinchGestureRecognizer *)thePinchGesture{ UIImageVIEw *thePanImageVIEw = (ID)thePinchGesture.vIEw; static CGRect theInitialFrame; if (thePinchGesture.state == UIGestureRecognizerStateBegan) { theInitialFrame = thePanImageVIEw.frame; } else { CGRect theFrame = theInitialFrame; theFrame.size.wIDth *= thePinchGesture.scale; theFrame.size.height *= thePinchGesture.scale; thePanImageVIEw.frame = theFrame; } thePanImageVIEw.layer.cornerRadius = thePanImageVIEw.frame.size.wIDth/2; UIImageVIEw *theImageVIEw = thePanImageVIEw.theWeakObject; thePanImageVIEw.center = [thePinchGesture locationInVIEw:theImageVIEw]; UIImageVIEw *theExtraimageVIEw = thePanImageVIEw.subvIEws.firstObject; theExtraimageVIEw.frame = CGRectMake(-thePanImageVIEw.frame.origin.x,theExtraimageVIEw.frame.size.height);} 供参考:
weakObject只是我的NSObject的自定义属性.我使用它,因为我很懒惰,不想创建全局可见的@property
总结以上是内存溢出为你收集整理的ios – 根据UIPinch缩放图像全部内容,希望文章能够帮你解决ios – 根据UIPinch缩放图像所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)