Fix the potential leak of CFDataRef

This commit is contained in:
DreamPiggy 2018-02-21 20:25:30 +08:00
parent 3e3ec8d513
commit 57408d8313
2 changed files with 6 additions and 3 deletions

View File

@ -124,7 +124,8 @@ typedef NS_OPTIONS(NSUInteger, SDRectCorner) {
- (nullable UIImage *)sd_tintedImageWithColor:(nonnull UIColor *)tintColor;
/**
Return the color at specify pixel. The postion is from the top-left to the bottom-right. And the color is always be RGBA format.
Return the color at specify pixel. The point is from the top-left to the bottom-right and 0-based. The returned the color is always be RGBA format.
@note The point's x/y should not be smaller than 0, or greater than or equal to width/height.
@param point The position of pixel
@return The color for specify pixel, or nil if any error occur

View File

@ -396,7 +396,7 @@ static CGRect SDCGRectFitWithScaleMode(CGRect rect, CGSize size, SDImageScaleMod
// Check point
CGFloat width = CGImageGetWidth(imageRef);
CGFloat height = CGImageGetHeight(imageRef);
if (point.x < 0 || point.y < 0 || point.x > width || point.y > height) {
if (point.x < 0 || point.y < 0 || point.x >= width || point.y >= height) {
return nil;
}
@ -411,15 +411,17 @@ static CGRect SDCGRectFitWithScaleMode(CGRect rect, CGSize size, SDImageScaleMod
}
// Get pixel at point
size_t bytesPerRow = CGImageGetBytesPerRow(imageRef); // Actually should be ARGB8888, equal to width * 4(alpha) or 3(non-alpha)
size_t bytesPerRow = CGImageGetBytesPerRow(imageRef);
size_t components = CGImageGetBitsPerPixel(imageRef) / CGImageGetBitsPerComponent(imageRef);
CFRange range = CFRangeMake(bytesPerRow * point.y + components * point.x, 4);
if (CFDataGetLength(data) < range.location + range.length) {
CFRelease(data);
return nil;
}
UInt8 pixel[4] = {0};
CFDataGetBytes(data, range, pixel);
CFRelease(data);
// Convert to color
CGImageAlphaInfo alphaInfo = CGImageGetAlphaInfo(imageRef);