您已经在所有像素上创建了一个双循环:indexOfObjectsPassingTest:
如果您在找到像素后未设置*stop
为,则继续。YES
也许这就是让它如此缓慢的原因?
我的方法是在一个方向添加阴影并绘制,然后在另一个方向添加阴影并绘制。
CGFloat width = image.size.width;
CGFloat height = image.size.height;
UIGraphicsBeginImageContext(CGSizeMake(width, height));
CGContextRef context = UIGraphicsGetCurrentContext();
// shadow in one direction
CGSize offset = CGSizeMake(2,2);
CGFloat blur = 0;
CGColorRef color = [UIColor blackColor].CGColor;
CGContextSetShadowWithColor(context, offset, blur, color);
CGContextDrawImage(context, CGRectMake(0, 0, width, height), image.CGImage);
// shadow in the other direction
CGSize offset2 = CGSizeMake(-2,-2);
CGContextSetShadowWithColor ( context, offset2, blur, color);
CGContextDrawImage(context, CGRectMake(0, 0, width, height), image.CGImage);
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
CGContextRelease(context);
另一种方法是绘制缩放图像并将其与黑色填充混合,然后在顶部绘制原始图像:
CGRect imageFrame = CGRectMake(105, 20, 108, 159);
UIImageView *imageView = [[UIImageView alloc] initWithFrame:imageFrame];
NSLog(@"imageView=%@", imageView);
UIImage *image = [UIImage imageNamed:@"mapicon-1.png"];
[imageView setImage:image];
[self.view addSubview:imageView];
CGRect newImageFrame = CGRectInset(imageFrame, -20, -20);
CGRect newImageBounds = CGRectMake(0, 0, newImageFrame.size.width, newImageFrame.size.height);
UIGraphicsBeginImageContext(newImageFrame.size);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextTranslateCTM(context, 0, newImageBounds.size.height);
CGContextScaleCTM(context, 1, -1);
CGContextSaveGState(context);
CGContextDrawImage(context, newImageBounds, image.CGImage);
CGContextSetBlendMode(context, kCGBlendModeSourceAtop);
CGContextSetFillColorWithColor(context, [UIColor blackColor].CGColor);
CGContextFillRect(context, newImageBounds);
CGContextRestoreGState(context);
CGContextDrawImage(context, CGRectInset(newImageBounds, 20, 20), image.CGImage);
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIImageView *newImageView = [[UIImageView alloc] initWithFrame:CGRectMake(105, 210, 108, 159)];
[newImageView setImage:newImage];
[self.view addSubview:newImageView];