IOS 超级无敌iOS基础·OC高级篇iOS开发-高级汇总

iOS中使用OpenGL 实现增高功能

2017-08-17  本文已影响216人  茉莉儿

功能效果

demo示例

功能分析

功能实现

//顶点数组
GLfloat vertices[] = {   
    -1.2, -1.2,     //左下
    1.2, -1.2,      //右下
    -1.2, -0.4,     //小矩形左下
    1.2, -0.4,      //小矩形右下
    -1.2,  0.4,     //小矩形左上
    1.2,  0.4,      //小矩形右上
    -1.2,  1.2,     //左上
    1.2,  1.2,      //右上
};

 //填充纹理的数组
GLfloat texCoords[] = {     
    0, 0,        //左下                  //下标为 0 1
    1, 0,        //右下                  //下标为2 3
    0, 1.0/3.0,  //小矩形左下             //下标为4 5
    1, 1.0/3.0,  //小矩形右下             //下标为6 7
    0, 2.0/3.0,  //小矩形左上角           //下标为8 9
    1, 2.0/3.0,  //小矩形右上角           //下标为10 11
    0, 1,        //左上                  //下标为12 13
    1, 1,        //右上                  //下标为14 15
};

*****************CustomPanView代码******************
#import <UIKit/UIKit.h>

@protocol CustomPanViewDelegate <NSObject>

/**
 *  开始拖拽
 *
 *  @param customPanView 自身
 *  @param centerY       自身所在的y坐标
 */
- (void)beginDragWithCoustomPanView:(UIView *)customPanView centerY:(CGFloat)centerY;

@end

@interface CustomPanView : UIView

@property (nonatomic, assign) id<CustomPanViewDelegate> delegate;

@end

*****************CustomPanViewDelegate代码******************

#pragma mark -
#pragma mark 拖拽View的代理方法
-(void)beginDragWithCoustomPanView:(UIView *)customPanView centerY:(CGFloat)centerY {
    
    // 限制范围:裁剪区不能大于图片区域
    if (customPanView.center.y >= imageBottom) {
        customPanView.center = CGPointMake(customPanView.center.x, imageBottom);
    }
    if (customPanView.center.y <= imageTop) {
        customPanView.center = CGPointMake(customPanView.center.x, imageTop);
    }
    
//    获取两条线的坐标
    CGFloat topY = _topView.center.y;
    
    CGFloat bottomY = _bottomView.center.y;
    
//    根据两条线的坐标刷新裁剪区域UI
    [_cutLabel setFrame:CGRectMake(0, topY < bottomY ? topY : bottomY, SCREEN_WIDTH, fabs(bottomY - topY))];
    
//    算出裁剪起始坐标和结束坐标
    CGFloat fromPoint = topY < bottomY ? (imageBottom - bottomY) / imageHeight : (imageBottom - topY) / imageHeight;
    
    CGFloat toPoint = topY < bottomY ? (imageBottom - topY) / imageHeight : (imageBottom - bottomY) / imageHeight;
    
   //将中间的矩形的顶点坐标和坐标联系裁剪区域联系起来。
    [self sendFromePoint:fromPoint endPoint:toPoint];
    
    
    
    if (_cutLabel.frame.size.height < 30) {  //隐藏文字
        _cutLabel.text = @"";
    } else {
        _cutLabel.text = @"编辑区域";
    }
    
    [self.slider setValue:0.0 animated:YES];
    
    tmpHeight = 0.0f;
    
}

使用一个Delegate将拖移后的Y坐标返回,因为是竖直运动的所以我们只关心Y轴坐标。

- (void)action:(UISlider *)sender {
    //判断是否是向右滑动
    isRightDirection = sender.value >= judgeDirection ? YES : NO;
    //所改变的高度
    changeHeight = sender.value - tmpHeight;

    //遍历数组
    for (int i = 0; i < 16; i ++) {
        //将Y坐标筛选出来
        if (i % 2 != 0) {
            //下半部分矩形
            if (i <= 7) {
                //下半部分矩形Y轴做减法减去变化的高度
                vertices[i] = verticesCopy[i] - changeHeight;
                //上半部分矩形
            } else if (i >= 9) {
                //上半部分矩形Y轴做加法加上变化的高度
                vertices[i] = verticesCopy[i] + changeHeight;
                
            }
            
        }
        
    }
    //缩小时候如果编辑区域已经成为一条线了就不能在缩小了
    if (vertices[11] > vertices[7]) {
    
        [self.glView display];
        
    }
    
}

#pragma mark -
#pragma mark 获取处理后的图片
- (UIImage *) createImage {
    
    int imageY = 0;
    
    int imgHeight = 0;
    
    if (isRightDirection) { // 判断slider滑动方向
        
        imageY = fabs(imageTop - fabs(changeHeight * perOpengleseCoordinate)) * screenScale;

        imgHeight = fabs(imageHeight + 2 * fabs(changeHeight * perOpengleseCoordinate)) * screenScale;
        
    } else {
        
        imageY = fabs(imageTop + fabs(changeHeight * perOpengleseCoordinate)) * screenScale;
        
        imgHeight = fabs(imageHeight - 2 * fabs(changeHeight * perOpengleseCoordinate)) * screenScale;
        
    }

    
    int imageWidth = SCREEN_WIDTH * screenScale;
    
    int dataLength = imageWidth * imgHeight * 4;
    
    GLubyte *data = (GLubyte*)malloc(dataLength * sizeof(GLubyte));
    
    glPixelStorei(GL_PACK_ALIGNMENT, 4);
    
    glReadPixels(0, imageY, imageWidth, imgHeight, GL_RGBA, GL_UNSIGNED_BYTE, data);  //从内存中读取像素
    CGDataProviderRef ref = CGDataProviderCreateWithData(NULL, data, dataLength, NULL);
    CGColorSpaceRef colorspace = CGColorSpaceCreateDeviceRGB();
    CGImageRef iref = CGImageCreate(imageWidth, imgHeight, 8, 32, imageWidth * 4, colorspace, kCGBitmapByteOrder32Big | kCGImageAlphaPremultipliedLast,ref, NULL, true, kCGRenderingIntentDefault);
    
    UIGraphicsBeginImageContext(CGSizeMake(imageWidth, imgHeight));
    CGContextRef cgcontext = UIGraphicsGetCurrentContext();
    CGContextSetBlendMode(cgcontext, kCGBlendModeCopy);
    CGContextDrawImage(cgcontext, CGRectMake(0, 0, imageWidth, imgHeight), iref);
    
    CGImageRef imageMasked = CGBitmapContextCreateImage(cgcontext);
    UIImage * image = [UIImage imageWithCGImage:imageMasked scale:screenScale orientation:UIImageOrientationUp];
    UIGraphicsEndImageContext();
    
    free(data);
    CFRelease(ref);
    CFRelease(colorspace);
    CGImageRelease(iref);
   
    return image;
    
}

上一篇下一篇

猜你喜欢

热点阅读