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:@"保存失敗"];

    }

}



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