iOS图形绘制方法汇总及图片处理

1、使用UIBezierPath 绘制图形 http://my.oschina.net/lanrenbar/blog/389379 http://justsee.iteye.com/blog/1972853

2.、使用CGContextRef进行图形绘制  http://blog.csdn.net/rhljiayou/article/details/9919713

3、使用CAGradientLayer可以方便的处理颜色渐变

4、画虚线用CAShapeLayer

图片处理

1、按某个区域对图片进行放大处理

userLine.image = image?.resizableImageWithCapInsets(UIEdgeInsetsMake(0, 150, 10, 10), resizingMode: UIImageResizingMode.Stretch)

2、图片任意角度旋转

//图片旋转任意角度
CGFloat DegreesToRadians(CGFloat degrees) {return degrees * M_PI / 180;};
CGFloat RadiansToDegrees(CGFloat radians) {return radians * 180/M_PI;};

- (UIImage *)imageRotatedByDegrees:(CGFloat)degrees
{
    // calculate the size of the rotated view's containing box for our drawing space
    UIView *rotatedViewBox = [[UIView alloc] initWithFrame:CGRectMake(0,0,20, 20)];
    CGAffineTransform t = CGAffineTransformMakeRotation(DegreesToRadians(degrees));
    rotatedViewBox.transform = t;
    CGSize rotatedSize = rotatedViewBox.frame.size;
    
    // Create the bitmap context
    UIGraphicsBeginImageContext(rotatedSize);
    CGContextRef bitmap = UIGraphicsGetCurrentContext();
    
    // Move the origin to the middle of the image so we will rotate and scale around the center.
    CGContextTranslateCTM(bitmap, rotatedSize.width/2, rotatedSize.height/2);
    
    //   // Rotate the image context
    CGContextRotateCTM(bitmap, DegreesToRadians(degrees));
    
    // Now, draw the rotated/scaled image into the context
    CGContextScaleCTM(bitmap, 1.0, -1.0);
    CGContextDrawImage(bitmap, CGRectMake(-20 / 2, -20 / 2, 20, 20), [UIImage imageNamed:@"1(2)@2x"].CGImage);
    
    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return newImage;
    
}

3、图片固定角度旋转

// 2D仿射变换

   只能旋转一次(make)

    _aView.transform = CGAffineTransformMakeRotation(M_PI_4);

   //可以实现多次旋转.(make),一般都是成对出现 

   _aView.transform = CGAffineTransformRotate(_aView.transform, M_PI_4);

   //实现动画,平缓过多使用Block语法

    [UIViewanimateWithDuration:0.5animations:^{

       _aView.transform =CGAffineTransformRotate(_aView.transform,M_PI_4);

    }];

//3D放射变换  1.导入QuartzCore框架  2.引入头文件#import <QuartzCore/CATransform3D.h>

 _imageView.layer.transform =CATransform3DRotate(_imageView.layer.transform,M_PI_4, 0,0, 1);

4、图片保存到本地相册

-(BOOL)savePhotoAlbum:(UIImage *)image

{

    ALAuthorizationStatus status =  [ALAssetsLibrary authorizationStatus];

    if (status == ALAuthorizationStatusAuthorized)

    {

        UIImageWriteToSavedPhotosAlbum(image, self, @selector(image:didFinishSavingWithError:contextInfo:), nil);

        

    }

    else if(status == ALAuthorizationStatusNotDetermined)

    {

        ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];

        [library enumerateGroupsWithTypes:ALAssetsGroupLibrary usingBlock:nil failureBlock:nil];

        [library enumerateGroupsWithTypes:ALAssetsGroupAlbum usingBlock:^(ALAssetsGroup *group, BOOL *stop) {

            UIImageWriteToSavedPhotosAlbum(image, self, @selector(image:didFinishSavingWithError:contextInfo:), nil);

        } failureBlock:^(NSError *error) {

            [SVProgressHUD showErrorWithStatus:@"未授权杏仁医生访问相册,保存失败"];

        }];

        //

    }

    else {

        UIAlertView *alert =  [[UIAlertView alloc] initWithTitle:@"请授权" message:@"请至 设置->隐私->照片 设置允许访问通相册" delegate:self cancelButtonTitle:@"确定" otherButtonTitles: nil];

        [alert show];

    }

    

    return true;

}


-(void)image:(UIImage *)imaged didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo {

    if(!error) {

        [SVProgressHUD showSuccessWithStatus:@"成功保存到相册"];

    }

    else {

        [SVProgressHUD showErrorWithStatus:@"保存失败"];

    }

}



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章