图片剪切

作者: 952625a28d0d | 来源:发表于2016-05-17 17:23 被阅读84次
p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px 'Hiragino Sans GB'; color: #008400}p.p2 {margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px 'Hiragino Sans GB'}p.p3 {margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px 'Hiragino Sans GB'; min-height: 27.0px}p.p4 {margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px 'Hiragino Sans GB'; color: #3d1d81}p.p5 {margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px 'Hiragino Sans GB'; color: #d12f1b}span.s1 {font-variant-ligatures: no-common-ligatures}span.s2 {font-variant-ligatures: no-common-ligatures; color: #703daa}span.s3 {font-variant-ligatures: no-common-ligatures; color: #bb2ca2}span.s4 {font-variant-ligatures: no-common-ligatures; color: #011993}span.s5 {font-variant-ligatures: no-common-ligatures; color: #3d1d81}span.s6 {font-variant-ligatures: no-common-ligatures; color: #78492a}span.s7 {font-variant-ligatures: no-common-ligatures; color: #008400}span.s8 {font-variant-ligatures: no-common-ligatures; color: #000000}

//图片压缩到指定大小
- (UIImage*)imageByScalingAndCroppingForSize:(CGSize)targetSize withImage:(UIImage *)image
{
    UIImage *sourceImage = image;
    UIImage *newImage = nil;
    CGSize imageSize = sourceImage.size;
    CGFloat width = imageSize.width;
    CGFloat height = imageSize.height;
    CGFloat targetWidth = targetSize.width;
    CGFloat targetHeight = targetSize.height;
    CGFloat scaleFactor = 0.0;
    CGFloat scaledWidth = targetWidth;
    CGFloat scaledHeight = targetHeight;
    CGPoint thumbnailPoint = CGPointMake(0.0,0.0);
    
    if (CGSizeEqualToSize(imageSize, targetSize) == NO)
    {
        CGFloat widthFactor = targetWidth / width;
        CGFloat heightFactor = targetHeight / height;
        
        if (widthFactor > heightFactor)
            scaleFactor = widthFactor; // scale to fit height
        else
            scaleFactor = heightFactor; // scale to fit width
        scaledWidth= width * scaleFactor;
        scaledHeight = height * scaleFactor;
        
        // center the image
        if (widthFactor > heightFactor)
        {
            thumbnailPoint.y = (targetHeight - scaledHeight) * 0.5;
        }
        else if (widthFactor < heightFactor)
        {
            thumbnailPoint.x = (targetWidth - scaledWidth) * 0.5;
        }
    }
    
    UIGraphicsBeginImageContext(targetSize); // this will crop
    
    CGRect thumbnailRect = CGRectZero;
    thumbnailRect.origin = thumbnailPoint;
    thumbnailRect.size.width= scaledWidth;
    thumbnailRect.size.height = scaledHeight;
    
    [sourceImage drawInRect:thumbnailRect];
    
    newImage = UIGraphicsGetImageFromCurrentImageContext();
    if(newImage == nil)
        NSLog(@"could not scale image");
    
    //pop the context to get back to the default
    UIGraphicsEndImageContext();
    return newImage;
}```

p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px 'Hiragino Sans GB'}span.s1 {font-variant-ligatures: no-common-ligatures}span.s2 {font-variant-ligatures: no-common-ligatures; color: #bb2ca2}span.s3 {font-variant-ligatures: no-common-ligatures; color: #011993}span.s4 {font-variant-ligatures: no-common-ligatures; color: #703daa}span.s5 {font-variant-ligatures: no-common-ligatures; color: #31595d}span.s6 {font-variant-ligatures: no-common-ligatures; color: #3d1d81}

if (videoPage.thumbnail) {
UIImage *image = [self imageByScalingAndCroppingForSize:CGSizeMake(100, 100) withImage:videoPage.thumbnail];
NSData * thumbData = UIImageJPEGRepresentation(image, 0.8);
message.thumbData = thumbData;
}```

相关文章

网友评论

    本文标题:图片剪切

    本文链接:https://www.haomeiwen.com/subject/ozpirttx.html