解决 UIView 设置背景为UIImage图片变型问题[XXX setBackgroundColor:

 [self.drawingView setBackgroundColor:[UIColor colorWithPatternImage:[self thumbnailWithImageWithoutScale:imagesize:self.drawingView.frame.size]]];

 

- (UIImage *)thumbnailWithImageWithoutScale:(UIImage *)image size:(CGSize)asize

{

    UIImage *newimage;

    if (nil == image)

    {

        newimage = nil;

    }

    else

    {

        CGSize oldsize = image.size;

        CGRect rect;

        if (asize.width/asize.height > oldsize.width/oldsize.height)

        {

            rect.size.width = asize.height*oldsize.width/oldsize.height;

            rect.size.height = asize.height;

            rect.origin.x = (asize.width - rect.size.width)/2;

            rect.origin.y = 0;

        }

        else

        {

            rect.size.width = asize.width;

            rect.size.height = asize.width*oldsize.height/oldsize.width;

            rect.origin.x = 0;

            rect.origin.y = (asize.height - rect.size.height)/2;

        }

        UIGraphicsBeginImageContext(asize);

        CGContextRef context = UIGraphicsGetCurrentContext();

        CGContextSetFillColorWithColor(context, [[UIColorclearColor] CGColor]);

        UIRectFill(CGRectMake(0, 0, asize.width, asize.height));//clear background

        [image drawInRect:rect];

        newimage = UIGraphicsGetImageFromCurrentImageContext();

        UIGraphicsEndImageContext();

    }

    return newimage;

}

转载:http://www.cnblogs.com/Camier-myNiuer/archive/2013/12/03.html




Animating UIImageView with colorWithPatternImage


Leave your code as it is, but instead using UIImageView use my subclass:

//
//  AnimatedPatternView.h
//

#import <UIKit/UIKit.h>

@interface AnimatedPatternView : UIImageView;    
@end

//
//  AnimatedPatternView.m
//

#import "AnimatedPatternView.h"

@implementation AnimatedPatternView

-(void)setAnimationImages:(NSArray *)imageArray
{
    NSMutableArray* array = [NSMutableArray arrayWithCapacity:imageArray.count];

    for (UIImage* image in imageArray) {
        UIGraphicsBeginImageContextWithOptions(self.bounds.size, YES, 0);
        UIColor* patternColor = [UIColor colorWithPatternImage:image];
        [patternColor setFill];
        CGContextRef ctx = UIGraphicsGetCurrentContext();
        CGContextFillRect(ctx, self.bounds);
        UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
        [array addObject:img];

    }
    [super setAnimationImages:array];
}

@end

If you create the view using interface builder you only need to set the class of the image view in the identity inspector.


转载:http://stackoverflow.com/questions/16461469/animating-uiimageview-with-colorwithpatternimage

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值