现在的位置: 首页 > 综合 > 正文

截屏和图片旋转

2017年12月28日 ⁄ 综合 ⁄ 共 2369字 ⁄ 字号 评论关闭

//截屏

-(void)writeFile

{

   
//系统沙盒路径

   NSArray *paths =NSSearchPathForDirectoriesInDomains(NSCachesDirectory,NSUserDomainMask,YES);

   
NSString
*cachesDirectory = [paths objectAtIndex:0];

    

   
NSString
*imgFilePath =nil;

   if (UI_USER_INTERFACE_IDIOM()==UIUserInterfaceIdiomPad)
{

        imgFilePath = [[NSStringalloc]
initWithFormat:@"%@/myScreen_iPad.png",cachesDirectory]; 

    }

   
else
{

        imgFilePath = [[NSStringalloc]
initWithFormat:@"%@/myScreen_iPhone.png",cachesDirectory];

    }

        

   
UIImage
*image = [self
screenshot
];

   if (UI_USER_INTERFACE_IDIOM()==UIUserInterfaceIdiomPad)
{

        image = [selfrotateUIImage:image
angle:270.0];

    }

   

     //UIImagePNGRepresentation转换PNG格式的图片为二进制,如果图片的格式为JPEG则返回nil;

         //UIImageJPEGRepresentation转换JPG格式的图片为二进制,如果图片的格式为JPEG则返回nil;

    NSData *myData = [NSDatadataWithData:UIImagePNGRepresentation(image)
];
   //writeToFile

   
if
(myData!=nil) {

        [myData
writeToFile
:imgFilePath atomically:YES];

    }

}

//从文件中取出
{

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory,NSUserDomainMask,YES);

    NSString *cachesDirectory = [paths objectAtIndex:0];

    NSString*imgFilePath =nil;

    

   
if
(UI_USER_INTERFACE_IDIOM()==UIUserInterfaceIdiomPad) {

        imgFilePath = [[NSString alloc] initWithFormat:@"%@/myScreen_iPad.png",cachesDirectory]; 

    }

   
else
{

        imgFilePath = [[NSString alloc] initWithFormat:@"%@/myScreen_iPhone.png",cachesDirectory];

    }

   
if
(imgFilePath) {

        UIImage *image = [UIImage imageWithContentsOfFile:imgFilePath];

        [imageview setImage:image];

    }

}
//图片旋转

-(UIImage*)rotateUIImage:(UIImage*) src angle:(float) angleDegrees {   

   
UIView
* rotatedViewBox = [[UIView
alloc
] initWithFrame:
CGRectMake
(0,
0
, src.size.width, src.size.height)];

   
float
angleRadians = angleDegrees * ((float)M_PI /180.0f);

   //float angleRadians = angleDegrees * ((float)M_PI / 10.0f);

   CGAffineTransform t =CGAffineTransformMakeRotation(angleRadians);

    rotatedViewBox.transform = t;

   
CGSize
rotatedSize = rotatedViewBox.frame.size;

   //[rotatedViewBox release];

    

   UIGraphicsBeginImageContext(rotatedSize);

   CGContextRef bitmap =UIGraphicsGetCurrentContext();

   
CGContextTranslateCTM
(bitmap, rotatedSize.width/2, rotatedSize.height/2);

   
CGContextRotateCTM
(bitmap, angleRadians);

    

   
CGContextScaleCTM
(bitmap, 1.0, -1.0);

   
CGContextDrawImage
(bitmap, CGRectMake(-src.size.width /2, -src.size.height
/2, src.size.width, src.size.height), [srcCGImage]);

    

   UIImage *newImage =UIGraphicsGetImageFromCurrentImageContext();

   UIGraphicsEndImageContext();

    

   
return
newImage;

}

抱歉!评论已关闭.