2017年12月12日 星期二

取得webview的內容size

https://stackoverflow.com/questions/3936041/how-to-determine-the-content-size-of-a-uiwebview
 
- (void)webViewDidFinishLoad:(UIWebView *)aWebView {
    CGRect frame = aWebView.frame;
    frame.size.height = 1;
    aWebView.frame = frame;
    CGSize fittingSize = [aWebView sizeThatFits:CGSizeZero];
    frame.size = fittingSize;
    aWebView.frame = frame;

    NSLog(@"size: %f, %f", fittingSize.width, fittingSize.height);
}
 
要先設height到很小,再做[webView sizeToFit]; 或 [webView sizeThatFits:CGSizeZero];

2017年12月5日 星期二

正方形圖片

+ (UIImage *)resizeToSquareImageWithImage:(UIImage*)image toSize:(CGSize)newSize
{
    // crop image to square one
    CGSize oldSize = image.size;
    double newWidth = oldSize.width > oldSize.height ? oldSize.height : oldSize.width;
    CGRect newRect = CGRectMake(0.0, 0.0, newWidth, newWidth);

    CGImageRef sourceImageRef = [image CGImage];
    CGImageRef newImageRef = CGImageCreateWithImageInRect(sourceImageRef, newRect);
    UIImage *squareImage = [UIImage imageWithCGImage:newImageRef];
    CGImageRelease(newImageRef);
    // resize the image
    UIGraphicsBeginImageContext(newSize);
    [squareImage drawInRect:CGRectMake(0, 0, newSize.width, newSize.height)];
    UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
  
    return newImage;
}

從data查圖片格式

source: https://stackoverflow.com/questions/4147311/finding-image-type-from-nsdata-or-uiimage 
 
+ (NSString *)contentTypeForImageData:(NSData *)data {
    uint8_t c;
    [data getBytes:&c length:1];

    switch (c) {
    case 0xFF:
        return @"image/jpeg";
    case 0x89:
        return @"image/png";
    case 0x47:
        return @"image/gif";
    case 0x49:
    case 0x4D:
        return @"image/tiff";
    }
    return nil;
}

2017年11月30日 星期四