I am trying to create a flexible frame for my iPhone application with small images. CustomView inherits from UIView and overrides its setFrame:
In setFrame :, I'm trying to call [self setNeedsDisplay];
. Every time I scale a photo, this frame really displays and changes, but something does not work very well. Code and effect below:
// rotate to get a mirror image of the image source, and isHorization means the direction of rotation - (UIImage *) fetchMirrorImage: (UIImage *) originImage direction: (BOOL) isHorization {CGSize imageSize = originImage.size; UIGraphicsBeginImageContext (IMAGESIZE); CGContextRef context = UIGraphicsGetCurrentContext (); CGAffineTransform transform = CGAffineTransformIdentity;
if (isHorization) { transform = CGAffineTransformMakeTranslation(imageSize.width, 0.0); transform = CGAffineTransformScale(transform, -1.0, 1.0); }else { transform = CGAffineTransformMakeTranslation(0.0, imageSize.height); transform = CGAffineTransformScale(transform, 1.0, -1.0); } CGContextScaleCTM(context, 1, -1); CGContextTranslateCTM(context, 0, -imageSize.height); CGContextConcatCTM(context, transform); CGContextDrawImage(context, CGRectMake(0, 0, imageSize.width, imageSize.height), originImage.CGImage); UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); return newImage; } - (UIImage*)fetchPattern:(PatternType)pattern{ if (!self.patternImage) { return nil; } UIImage *tmpPattern = nil; CGRect fetchRect = CGRectZero; CGSize imageSize = self.patternImage.size; switch (pattern) { case kTopPattern: fetchRect = CGRectMake(self.insetSize.width, 0, imageSize.width-self.insetSize.width, self.insetSize.height); tmpPattern = [UIImage imageWithCGImage:CGImageCreateWithImageInRect(self.patternImage.CGImage, fetchRect)]; break; case kTopRightPattern: fetchRect = CGRectMake(0, 0, self.insetSize.width, self.insetSize.height); tmpPattern = [UIImage imageWithCGImage:CGImageCreateWithImageInRect(self.patternImage.CGImage, fetchRect)]; break; case kRightPattern: break; case kRightBottomPattern: break; case kBottomPattern: break; case kLeftBottomPattern: break; case kLeftPattern: fetchRect = CGRectMake(0, self.insetSize.height, self.insetSize.width, imageSize.height-self.insetSize.height); tmpPattern = [UIImage imageWithCGImage:CGImageCreateWithImageInRect(self.patternImage.CGImage, fetchRect)]; break; case kLeftTopPattern: fetchRect = CGRectMake(0, 0, self.insetSize.width, self.insetSize.height); tmpPattern = [UIImage imageWithCGImage:CGImageCreateWithImageInRect(self.patternImage.CGImage, fetchRect)]; break; default: break; } return tmpPattern; }